강의

멘토링

커뮤니티

Inflearn Community Q&A

97091932717's profile image
97091932717

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

9. Breadth First Search (BFS)

dis 리스트 의 필요성

Written on

·

182

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)
python코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

네 맞습니다. 

97091932717's profile image
97091932717

asked

Ask a question