Skip to content

Commit 91b2506

Browse files
committed
[BOJ] #2178. 미로탐색 / 실버1 / 55분 / 힌트, 성공
1 parent 884e237 commit 91b2506

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import sys
2+
from collections import deque
3+
input = sys.stdin.readline
4+
5+
# 입력 처리
6+
N, M = map(int, input().split())
7+
graph = [list(map(int, input().strip())) for _ in range(N)]
8+
9+
def bfs(x, y):
10+
# 방향: 상, 하, 좌, 우
11+
dx = [-1, 1, 0, 0]
12+
dy = [0, 0, -1, 1]
13+
14+
queue = deque()
15+
queue.append((x, y))
16+
17+
while queue:
18+
x, y = queue.popleft()
19+
20+
for i in range(4):
21+
nx = x + dx[i]
22+
ny = y + dy[i]
23+
24+
# 범위 벗어나면 무시
25+
if nx < 0 or nx >= N or ny < 0 or ny >= M:
26+
continue
27+
28+
# 벽이면 무시
29+
if graph[nx][ny] == 0:
30+
continue
31+
32+
# 이동 가능한 칸이면 거리 갱신 후 방문 처리
33+
if graph[nx][ny] == 1:
34+
graph[nx][ny] = graph[x][y] + 1
35+
queue.append((nx, ny))
36+
37+
return graph[N-1][M-1]
38+
39+
print(bfs(0, 0))

0 commit comments

Comments
 (0)