DevYoon
[๋ฐฑ์ค] 21610. ๋ง๋ฒ์ฌ ์์ด์ ๋น๋ฐ๋ผ๊ธฐ (Python) ๋ณธ๋ฌธ
link ๐ https://www.acmicpc.net/problem/21610
โ๏ธ ๊ตฌ๋ฆ์ด ์ด๋ํ ๋ ์ฒซ๋ฒ์งธ ํ,์ด๊ณผ ๋ง์ง๋ง ํ,์ด์ด ์ฐ๊ฒฐ๋์ด ์์ง๋ง ๋๊ฐ์ ์ ๋ฐ๊ตฌ๋๋ฅผ ํ์ธํ ๋ ์ฐ๊ฒฐ๋์ด ์์ง ์๋ค๋ ๊ฒ์ ์ ์๊ฐํด์ผ ํ๋ ๋ฌธ์ ์๋ค.
โ๏ธ ์ฒ์ ํผ ์ฝ๋๋ก๋ ์๊ฐ์ด๊ณผ๊ฐ ๋ฌ๋ค. ๊ตฌ๋ฆ์ด ์ฌ๋ผ์ง ์นธ์ธ์ง ํ์ธํ๋ ๊ณผ์ ์์ not in์ ์ผ๋๋ฐ, ์ด๋ฅผ ๋ฐฐ์ด์ ํ๋ ๋ง๋ค์ด ์ฒดํฌํด์ฃผ๋ ์์ผ๋ก ๋ฐ๊พธ์ด ์๊ฐ์ด๊ณผ๋ฅผ ํด๊ฒฐํ๋ค.
๐ ์๊ฐ์ด๊ณผ ๋ ์ฝ๋
import sys
input = sys.stdin.readline
def movCloud(lst, direction, count):
for idx in range(len(lst)):
for num in range(2):
lst[idx][num] += dir[direction][num]*(count%N)
if lst[idx][num] >= N:
lst[idx][num] = lst[idx][num]%N
elif lst[idx][num] < 0:
lst[idx][num] = lst[idx][num]+N
return lst
def checkDgs(y, x):
isWater = 0
checkDir = [(-1, -1), (-1, 1), (1, 1), (1, -1)]
for i, j in checkDir:
dy, dx = y+i, x+j
if 0 <= dy < N and 0 <= dx < N:
if board[dy][dx] > 0:
isWater += 1
return isWater
N, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
move = [list(map(int, input().split())) for _ in range(M)] # ๋ฐฉํฅ, ๊ฑฐ๋ฆฌ
dir = [(0, 0), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] # 8๋ฐฉํฅ
cloud = [[N-1, 0], [N-1, 1], [N-2, 0], [N-2, 1]]
for idx in range(M):
clound = movCloud(cloud, move[idx][0], move[idx][1])
for y, x in clound:
board[y][x] += 1
for y, x in cloud:
board[y][x] += checkDgs(y, x)
newCloud = []
for y in range(N):
for x in range(N):
if board[y][x] >= 2 and [y, x] not in cloud:
newCloud.append([y, x])
board[y][x] -= 2
cloud = newCloud
res = 0
for idx in range(N):
res += sum(board[idx])
print(res)
๐ ์ ๋ต ์ฝ๋
import sys
input = sys.stdin.readline
def movCloud(lst, direction, count):
for idx in range(len(lst)):
for num in range(2):
lst[idx][num] += dir[direction][num]*(count%N) # if count๊ฐ 50์ด๊ณ N์ด 5์ด๋ฉด ์์ง์ผ ํ์ X
if lst[idx][num] >= N: # ์ฒ์๊ณผ ๋์ด ์ฐ๊ฒฐ๋์ด ์์ผ๋ฏ๋ก N์ด์์ผ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
lst[idx][num] = lst[idx][num]%N
elif lst[idx][num] < 0: # ๋์ผํ ์ด์ ๋ก ์์์ผ ๋ ์ฒ๋ฆฌ
lst[idx][num] = lst[idx][num]+N
return lst
def checkDgs(y, x): # ๋๊ฐ์ ์ค ๋ฐ๊ตฌ๋์ ๋ฌผ ์๋ ๊ฐ์ ๊ตฌํ๊ธฐ
isWater = 0
checkDir = [(-1, -1), (-1, 1), (1, 1), (1, -1)]
for i, j in checkDir:
dy, dx = y+i, x+j
if 0 <= dy < N and 0 <= dx < N:
if board[dy][dx] > 0:
isWater += 1
return isWater
N, M = map(int, input().split())
board = [list(map(int, input().split())) for _ in range(N)]
move = [list(map(int, input().split())) for _ in range(M)] # ๋ฐฉํฅ, ๊ฑฐ๋ฆฌ
dir = [(0, 0), (0, -1), (-1, -1), (-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1)] # 8๋ฐฉํฅ
cloud = [[N-1, 0], [N-1, 1], [N-2, 0], [N-2, 1]] # ์ด๊ธฐ ๊ตฌ๋ฆ ์์น
for idx in range(M):
clound = movCloud(cloud, move[idx][0], move[idx][1]) # ๊ตฌ๋ฆ ์์ง์ฌ์ฃผ๊ธฐ
checkCloud = [[0]*N for _ in range(N)] # ๊ตฌ๋ฆ ์๋ ๊ณณ ์ฒดํฌ ์ฉ๋
for y, x in clound:
board[y][x] += 1 # ๊ตฌ๋ฆ ์๋ ์นธ ๋ฐ๊ตฌ๋์ ๋ฌผ +1
checkCloud[y][x] = 1 # ๊ตฌ๋ฆ ์๋ค๊ณ ์ฒดํฌ
for y, x in cloud:
board[y][x] += checkDgs(y, x) # ๋๊ฐ์ ์ ๋ฌผ ์๋ ์๋งํผ ๋ฌผ ์ฆ๊ฐ
newCloud = []
for y in range(N):
for x in range(N):
if board[y][x] >= 2 and checkCloud[y][x] == 0: # ๋ฐ๊ตฌ๋์ ๋ฌผ์ด 2 ์ด์ ์๊ณ , ๊ตฌ๋ฆ ์๋ ์นธ์ด ์๋๋ฉด
newCloud.append([y, x]) # ์๋ก์ด ๊ตฌ๋ฆ ์นธ์ผ๋ก ์ถ๊ฐ
board[y][x] -= 2 # ๋ฌผ 2 ๊ฐ์
cloud = newCloud # ๊ตฌ๋ฆ ๋ฆฌ์คํธ ๊ต์ฒด
res = 0
for idx in range(N):
res += sum(board[idx]) # ๊ฐ ํ๋ง๋ค ๋ฌผ์ ์ ๋ํด์ฃผ๊ธฐ
print(res)