Skip to content

Commit 8ab447e

Browse files
committed
[BOJ]#2206. 벽 부수고 이동하기 / Gold3/실패
https://www.acmicpc.net/problem/2206
1 parent f95466f commit 8ab447e

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sys
2+
from collections import deque
3+
# 1. 입력 그래프
4+
N , M = map(int,sys.stdin.readline().split())
5+
graph = []
6+
for n in range(N) :
7+
graph.append(list(map(int, input())))
8+
9+
#방문 여부 & 시작노드로 부터 최단 경로 거리 ,벽 부순 횟수(3차원)
10+
visited = [[[0]*2 for _ in range(M)] for k in range(N)]
11+
12+
#상하좌우 -> 인접 노드
13+
dx = [0,0,1,-1]
14+
dy = [1,-1,0,0]
15+
16+
# 2. bfs 최단 거리
17+
def bfs():
18+
#초기 노드 queue 에 추가 , 방문 등록
19+
dq = deque()
20+
dq.append((0,0,0))
21+
visited[0][0][0] = 1
22+
23+
while dq :
24+
x,y,wall = dq.popleft()
25+
# 목적지(N,M) 도착한 경우,이동 횟수 출력
26+
if x == N-1 and y == M -1 :
27+
return visited[x][y][wall]
28+
29+
for i in range(4):#상하좌우로 다음 이동할 칸의 위치
30+
xx = x+ dx[i] ; yy = y+ dy[i]
31+
# (1)다음 이동할 곳이 graph 밖에 있는 경우
32+
if xx<0 or xx>= N or yy <0 or yy>= M:
33+
continue
34+
# (2) 다음 이동할 곳이 벽이고, 한번도 벽 안 뚤었을때
35+
if graph[xx][yy] == 1 and wall == 0 : # and not visited[xx][yy][1]
36+
visited[xx][yy][1] = visited[x][y][0]+1
37+
dq.append((xx,yy,1))
38+
39+
#(3)다음 이동할 곳이 벽이 아니고,한번도 방문 하지 않은 곳
40+
elif graph[xx][yy] == 0 and visited[xx][yy][wall] == 0 : # 그냥 감
41+
visited[xx][yy][wall] = visited[x][y][wall] +1
42+
dq.append((xx,yy,wall))
43+
44+
return -1 #BFS 종료될떄 까지 도착점에 도달하지 못하면 -1 출력하기
45+
46+
print(bfs())
47+
48+

_WeeklyChallenges/W04-[BFS]/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## 🚀 12월 3주차 (12/21) 스터디 발제 주제: BFS
1+
## 🚀 12월 3주차 (12/16) 스터디 발제 주제: BFS
22
> 발제자: 김홍주
33
44
### 🗂️ 스터디 자료

0 commit comments

Comments
 (0)