🏷 문제


💡 코드
from collections import deque
n = int(input())
space = []
for i in range(n):
space.append(list(map(int, input().split())))
case = [[-1, 0],[0, -1], [0, 1], [1, 0]]
shark_size = 2
def find(x,y):
visited = [[0 for _ in range(n)] for _ in range(n)]
visited[x][y] = 1
q = deque()
q.append([x, y])
res = []
while q:
x, y = q.popleft()
for i in range(4):
newx = x + case[i][0]
newy = y + case[i][1]
if 0 <= newx < n and 0 <= newy < n:
if space[newx][newy] <= shark_size and visited[newx][newy] == 0:
if space[newx][newy] < shark_size and space[newx][newy] != 0:
res.append([newx, newy, visited[x][y]])
else:
q.append([newx,newy])
visited[newx][newy] = visited[x][y] + 1
if len(res) == 0:
return -1, -1, -1
else:
res = sorted(res, key=lambda x: (x[2], x[0], x[1]))
return res[0]
time = 0
shark_cnt = 0
def eat_fish():
global space, shark_cnt, shark_size, time
for i in range(n):
for j in range(n):
if space[i][j] == 9:
sharkx, sharky, sec = find(i,j)
if sharkx == -1 and sharky == -1 and sec == -1:
return False
else:
shark_cnt += 1
time += sec
if shark_size == shark_cnt:
shark_size += 1
shark_cnt = 0
space[i][j] = 0
space[sharkx][sharky] = 9
return True
while True:
if eat_fish():
continue
else:
break
print(time)
🔑