티스토리 뷰

"""
 *packageName    : 
 * fileName       : 2178_미로탐색_S1
 * author         : ipeac
 * date           : 2022-04-13
 * description    :
 * ===========================================================
 * DATE              AUTHOR             NOTE
 * -----------------------------------------------------------
 * 2022-04-13        ipeac       최초 생성
 """
from collections import deque

def bfs(i, j):
      queue.append((i, j))
      visited[i][j] = 1
      while queue:
            x, y = queue.popleft()
            for k in range(4):
                  nx, ny = x + dx[k], y + dy[k]
                  if 0 <= nx < N and 0 <= ny < M and visited[nx][ny] == 0 and graph[nx][ny] == 1:
                        queue.append((nx, ny))
                        visited[nx][ny] = visited[x][y] + 1


N, M = map(int, input().split())
graph = [list(map(int, input())) for _ in range(N)]

visited = [[0] * M for _ in range(N)]

queue = deque()
dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]

bfs(0, 0)

print(visited[-1][-1])
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함