main 메소드 로직
- from collections import dequeue
- n,m,grpah[][] 입력받기
- print(bfs(0,0)) 호출 (가장아래줄)
[0,0]에서 상하좌우
X = [0,0,-1,1]
Y = [1,-,1,0,0]
상 하 좌 우
def bfs(x,y) 로직
- queue 큐를 만들기 (deque로)
- graph[x][y]를 append
- while (queue) : 큐가 빌때까지
popleft--> x,y
코드
- 아직 BFS 를 어떻게 짜야하는지 잘 감이 안온다,,
상하좌우 for i in range 4:
nx = x + X[i]
ny = y + Y[i]
if nx<0 or ny<0 or nx>=n or ny>=m :
continue
if graph[nx][y]==1:
continue
if graph[nx][ny]==0:
graph[nx][ny] = graph[x][y] + 1
return g[n-1][m-1]
'''
from collections import deque
X=[0,0,-1,1]
Y=[1,-1,0,0]
# 상 하 좌 우
n,m = map(int, input().split())
graph=[]
for i in range(n):
graph.append(list(map(int,input().split())))
def bfs(x,y):
#deque()를 이용해서 큐를 만든다
queue = deque()
queue.append((x,y))
while queue :
x,y = queue.popleft()
for i in range(4):
nx= x + X[i]
ny= y + Y[i]
if nx<0 or ny<0 or nx>=n or ny>=m :
continue
if graph[nx][ny]==0:
continue
if graph[nx][ny]==1:
graph[nx][ny] = graph[x][y] + 1
queue.append((nx,ny))
return graph[n-1][m-1]
print(bfs(0,0))
~~~