인프런 커뮤니티 질문&답변
dis 리스트 의 필요성
작성
·
186
1
안녕하세요 지난번 송아지찾기 질문과 동일한 질문입니다.
이 문제의 경우 input 데이터를 받는 이중리스트 하나만으로도 풀이가 가능할 것 같은데 dis 리스트를 별도로 만든 것은 편의상 추가한 것으로 봐도 될까요?
import sys
from collections import deque
def BFS(n):
dQ = deque()
dQ.append((0, 0))
while dQ:
tmp = dQ.popleft()
for i in range(4):
x = tmp[0]+dx[i]
y = tmp[1]+dy[i]
if 0<=x<=n-1 and 0<=y<=n-1 and a[x][y]==0:
a[x][y] = a[tmp[0]][tmp[1]]+1
dQ.append((x, y))
if a[n-1][n-1]==0:
print(-1)
else:
print(a[n-1][n-1])
if __name__=="__main__":
# with open('in6.txt') as sys.stdin:
n = 7
a = [list(map(int,sys.stdin.readline().split())) for _ in range(n)]
dx = [-1, 0, 1, 0]
dy = [0, 1, 0, -1]
BFS(n)




