Skip to content

Commit e821e5d

Browse files
committed
[BOJ] 벽 부수고 이동하기 / 골드 3 / 80분 실패
https://www.acmicpc.net/problem/2206
1 parent fb082c8 commit e821e5d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import deque
2+
3+
n, m = map(int, input().split())
4+
graph = []
5+
6+
visited = [[[0] * 2 for _ in range(m)] for _ in range(n)]
7+
visited[0][0][0] = 1
8+
9+
for i in range(n):
10+
graph.append(list(map(int, input())))
11+
12+
dx = [0, 0, 1, -1]
13+
dy = [1, -1, 0, 0]
14+
15+
16+
def bfs(x, y, z):
17+
queue = deque()
18+
queue.append((x, y, z))
19+
20+
while queue:
21+
a, b, c = queue.popleft()
22+
# 끝 점에 도달하면 이동 횟수를 출력
23+
if a == n - 1 and b == m - 1:
24+
return visited[a][b][c]
25+
for i in range(4):
26+
nx = a + dx[i]
27+
ny = b + dy[i]
28+
if nx < 0 or nx >= n or ny < 0 or ny >= m:
29+
continue
30+
# 다음 이동할 곳이 벽이고, 벽파괴 기회를 사용하지 않은 경우
31+
if graph[nx][ny] == 1 and c == 0 :
32+
visited[nx][ny][1] = visited[a][b][0] + 1
33+
queue.append((nx, ny, 1))
34+
# 다음 이동할 곳이 벽이 아니고, 아직 한 번도 방문하지 않은 곳이면
35+
elif graph[nx][ny] == 0 and visited[nx][ny][c] == 0:
36+
visited[nx][ny][c] = visited[a][b][c] + 1
37+
queue.append((nx, ny, c))
38+
return -1
39+
40+
41+
print(bfs(0, 0, 0))

0 commit comments

Comments
 (0)