문제 링크: https://www.acmicpc.net/problem/16236
백준 알고리즘 중급 1/3 611에서 12번째 - 16236번 아기 상어를 풀어보았다.
C++
Python
from collections import deque
N = int(input())
space = [list(map(int, input().split())) for _ in range(N)]
dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]
pos = []
for i in range(N):
for j in range(N):
if space[i][j] == 9:
pos.append(i)
pos.append(j)
cnt = 0
def bfs(x, y):
visited = [[0]*N for _ in range(N)]
queue = deque([[x,y]])
cand = []
visited[x][y] = 1
while queue:
i, j = queue.popleft()
for idx in range(4):
ii, jj = i + dx[idx] , j + dy[idx]
if 0 <= ii and ii < N and 0 <= jj and jj < N and visited[ii][jj] == 0:
if space[x][y] > space[ii][jj] and space[ii][jj] != 0:
visited[ii][jj] = visited[i][j] + 1
cand.append((visited[ii][jj] - 1, ii, jj))
elif space[x][y] == space[ii][jj]:
visited[ii][jj] = visited[i][j] + 1
queue.append([ii,jj])
elif space[ii][jj] == 0:
visited[ii][jj] = visited[i][j] + 1
queue.append([ii,jj])
return sorted(cand, key = lambda x: (x[0], x[1], x[2]))
i, j = pos
size = [2, 0]
while True:
space[i][j] = size[0]
cand = deque(bfs(i,j))
if not cand:
break
step, xx, yy = cand.popleft()
cnt += step
size[1] += 1
if size[0] == size[1]:
size[0] += 1
size[1] = 0
space[i][j] = 0
i, j = xx, yy
print(cnt)
Java
'코테용 문제풀이 > 백준' 카테고리의 다른 글
소수 경로 풀이 (0) | 2023.03.01 |
---|---|
레이저 통신 풀이 (0) | 2023.03.01 |
탈출 풀이 (0) | 2023.02.28 |
움직이는 미로 탈출 풀이 (0) | 2023.02.28 |
벽 부수고 이동하기 4 풀이 (0) | 2023.02.28 |