ν°μ€ν 리 λ·°
λ°±μ€: 20057 λ§λ²μ¬ μμ΄μ ν λ€μ΄λ
dirmathfl 2021. 3. 31. 23:09λ¬Έμ
λ¬Έμ νμ΄
5 x 5 ν¬κΈ°μ ν λ€μ΄λκ° λ°©ν₯μ μ ννλ©°, νμ¬ μλ λͺ¨λμ μν₯μ λ―ΈμΉλ λ¬Έμ μ΄λ€. λ¬Έμ λ₯Ό νκΈ° μν΄μλ `μ, ν, μ’, μ°`λ‘ μ νλλ κ²½μ°μ ν λ€μ΄λμ λν΄ μ¬μ μ μ΄κΈ°νκ° νμνλ€.
tornado = [
(None, None, 2, None, None),
(None, 10, 7, 1, None),
(5, 'a', None, None, None),
(None, 10, 7, 1, None),
(None, None, 2, None, None)
]
temp = [tornado]
for idx in range(3):
temp.append(list(reversed(list(zip(*temp[-1])))))
μμ κ°μ΄, λ¬Έμ μ μ£Όμ΄μ§ 쑰건μ λ§κ² `tornado` 리μ€νΈλ₯Ό μ μΈν ν, λ°μκ³ λ°©ν₯μΌλ‘ νμ μν¨λ€. κ·Έλ¦¬κ³ λ€μμ μ΄μ μ κ°μ κΈ°μ€μΌλ‘ λ€μ νμ μν€λ κ³Όμ μ 3λ² λ°λ³΅νκ² λλ©΄ `μ, ν, μ’, μ°`λ‘ μ νλλ ν λ€μ΄λλ₯Ό ꡬν μ μλ€.
ν λ€μ΄λμ μ΄κΈ°ν κ³Όμ μ΄ λλ¬λ€λ©΄, λ¨μν Flood fill λ¬Έμ μλ λ¬λ¦¬ λμ νμΌλ‘ λμλκ°λ©° νμμ μ§ννμ¬μΌ νλ€. λ¬Έμ μμ κ·μΉμ `μ’, ν, μ°, μ`μΌλ‘ μμ§μ΄λ©° μμ§μ΄λ 거리λ `ν → μ°`, `μ → μ’` μΈ κ²½μ°μ κ±°λ¦¬κ° 1μ© μ¦κ°νκ² λλ€. μ¦, μ κ·Έλ¦Όμμμ λΉ¨κ°μ νμ΄νμΈ κ²½μ°μ, λ Έλμ νμ΄νμΈ κ²½μ°μ μ΄λνλ κ±°λ¦¬κ° 1μ© μ¦κ°νλ€.
λλ¨Έμ§λ λ¬Έμ μμ μꡬνλ κ²κ³Ό κ°μ΄ λμνλλ‘ νλ€λ©΄ λ¬Έμ μμ μνλ λ΅μ μ°Ύμ μ μλ€.
μ½λ
from sys import stdin
from copy import deepcopy
def visitable(cur_x, cur_y):
return 0 <= cur_x < N and 0 <= cur_y < N
def init_tornado():
global tornado
temp = [tornado]
# anti-clockwise
for idx in range(3):
temp.append(list(reversed(list(zip(*temp[-1])))))
tornado = deepcopy(temp)
def spread(cur_tornado, loc):
global answer, A
cur_x, cur_y = loc
# tornado λ°°μ΄μ μμΉλ₯Ό μΌμΉμν€κΈ° μν΄ -2μ© κ°μ
tdx, tdy = cur_x - 2, cur_y - 2
# 'a'μ λ€μ΄κ° κ°μ λμ νκΈ° μν¨
a = 0
# 'a'μ μμΉλ₯Ό κΈ°λ‘
a_loc = None
for tx in range(5):
for ty in range(5):
nx, ny = tx + tdx, ty + tdy
# λͺ¨λμ μν₯μ λ―ΈμΉμ§ μλ κ²½μ°
if cur_tornado[tx][ty] is None:
continue
# μ«μλ‘, λͺ¨λκ° ν©λ λ € κ°λ κ²½μ°
elif cur_tornado[tx][ty] != 'a':
cur_sand = (A[cur_x][cur_y] * cur_tornado[tx][ty]) // 100
if visitable(nx, ny):
A[nx][ny] += cur_sand
else:
answer += cur_sand
a += cur_sand
# aμΈ κ²½μ°, κ°μ μ°¨νμ λ°μνκΈ° μν΄ κΈ°λ‘
else:
a_loc = (tx, ty)
# λμ λ 'a' κ°μ λ°μ
nx, ny = a_loc[0] + tdx, a_loc[1] + tdy
cur_sand = A[cur_x][cur_y] - a
if visitable(nx, ny):
A[nx][ny] += cur_sand
else:
answer += cur_sand
A[cur_x][cur_y] = 0
if __name__ == '__main__':
answer = 0
tornado = [
(None, None, 2, None, None),
(None, 10, 7, 1, None),
(5, 'a', None, None, None),
(None, 10, 7, 1, None),
(None, None, 2, None, None)
]
dirs = ((0, -1), (1, 0), (0, 1), (-1, 0))
N = int(stdin.readline())
A = [list(map(int, stdin.readline().split())) for _ in range(N)]
init_tornado()
dist = 1
x = y = N // 2
while True:
for cur_dir in range(4):
dx, dy = dirs[cur_dir]
for _ in range(dist):
next_x, next_y = x + dx, y + dy
# ν λ€μ΄λ μλ©Έ μ§μ
if (next_x, next_y) == (0, -1):
print(answer)
exit(0)
spread(tornado[cur_dir], [next_x, next_y])
x, y = next_x, next_y
# κ±°λ¦¬κ° μΆκ°λλ κ²μ 2, 4λ²μ§ΈμΈ κ²½μ°.
dist += 1 if cur_dir == 1 or cur_dir == 3 else 0
'π¨βπ» μ½λ©ν μ€νΈ > λ°±μ€' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
λ°±μ€: 17135 μΊμ¬ λνμ€ (0) | 2021.04.04 |
---|---|
λ°±μ€: 2186 λ¬Έμν (0) | 2021.04.02 |
λ°±μ€: 17140 μ΄μ°¨μ λ°°μ΄κ³Ό μ°μ° (0) | 2021.03.28 |
λ°±μ€: 8980 νλ°° (0) | 2021.03.19 |
λ°±μ€: 2141 μ°μ²΄κ΅ (0) | 2021.03.19 |