Part6.7_완전탐색_깊이,넓이 우선탐색활용_송아지 찾기(BFS)

BFS란??

선생님 코드

import sys
sys.stdin = open("input.txt", "rt")
from collections import deque

MAX = 100000
dis = [0] * (MAX+1)
ch = [0] * (MAX+1)
n,m = map(int,input().split())
dQ = deque()
dQ.append(n)

while dQ:
    now = dQ.popleft()
    if now == m :
        break
    for next in(now -1, now +1, now +5):
        if 0< next < MAX :
            if ch[next] == 0:
                dQ.append(next)
                ch[next]=1
                dis[next] = dis[now]+1
print(dis[m])
   

좋은 웹페이지 즐겨찾기