ch1019   3년 전

from collections import deque

n,m = map(int, input().split())

miro= []

for i in range(n):
miro.append(list(map(int,input())))

def bfs(x,y):
#이동방향
dx = [-1,1,0,0]
dy = [0,0,-1,1]

q = deque()
q.append((x,y))
while q:
x, y = q.popleft()

for i in range(4):
posx = x + dx[i]
posy = y + dx[i]

if posx <0 or posx >= n or posy <0 or posy >= n:
continue
if miro[posx][posy] == 0:
continue
if miro[posx][posy] == 1:
miro[posx][posy] = miro[x][y]+1
q.append((posx,posy))

return miro[n-1][m-1]

print(bfs(0,0))
이거 왜틀린가요



posy의 변화계산부분을 dx가 아닌 dy리스트를 사용해야합니다

미로의 범위를 검증하는 부분에서 posy는 미로의 열을 체크해야하므로 n이 아닌 m을 사용해야합니다

댓글을 작성하려면 로그인해야 합니다.