Agorithm/백준 알고리즘
백준 알고리즘 6229번 Bronze Lilypad Pond(python)
kimjinho1
2021. 3. 18. 23:53
728x90
반응형
기본적인 BFS 문제이다.
from collections import deque
def bfs(y, x):
q = deque()
q.append((y, x))
graph[y][x] = 0
while q:
y, x = q.popleft()
for dy, dx in d:
Y, X = y+dy, x+dx
if (0 <= Y < M) and (0 <= X < N):
if graph[Y][X] == '4':
return graph[y][x]+1
if graph[Y][X] == '1':
q.append((Y, X))
graph[Y][X] = graph[y][x]+1
M, N, M1, M2 = map(int, input().split())
graph = [input().split() for _ in range(M)]
d = [(-M1, -M2), (-M1, M2), (M1, -M2), (M1, M2), (-M2, -M1), (-M2, M1), (M2, -M1), (M2, M1)]
for i in range(M):
for j in range(N):
if graph[i][j] == '3':
print(bfs(i, j))
728x90
반응형