강의

멘토링

커뮤니티

Inflearn Community Q&A

pdh6941's profile image
pdh6941

asked

Introduction to Python Algorithm Problem Solving (Coding Test Preparation)

11. Hiking Route (DFS)

혹시 11. 등산 경로(BFS) 코드 알려주실 수 있을까요?

Written on

·

243

0

dx=[0,1,0,-1]
dy=[-1,0,1,0]

n = int(input())
board = [ [0]*n for i in range(n) ]
dq = deque()
MAX = -999
MIN = 999
cnt = 0

for i in range(n):  # 한 줄씩 입력 받기
    tmp = list(map(int,input().split()))
    for j in range(n):
        board[i][j] = tmp[j]

        if tmp[j] < MIN :
            MIN = tmp[j]
            sx =i
            sy =j
            dq.append([sx,sy])
        elif tmp[j]>MAX:
            MAX = tmp[j]
            ex = i
            ey = j

while dq:
    tmp = dq.popleft()

            #
    if tmp[0] == ex and tmp[1]==ey:            
        cnt+=1
               

    for a in range(4):
        X = dx[a]+tmp[0]
        Y = dy[a]+ tmp[1]

        if 0<=X<n and 0<=Y<n and board[X][Y] > board[tmp[0]][tmp[1]]   :
                    # board[X][Y] = 0
            dq.append([X,Y])
                       
print(cnt)
 
 
 
'''
해당 코드에서 뭐가 잘못된건지 집어주시면 감사하겠습니다.
'''
bfspython코테 준비 같이 해요!

Answer 1

0

codingcamp님의 프로필 이미지
codingcamp
Instructor

안녕하세요^^

등산경로는 가지수를 구하는 문제이므로 BFS로는 가능하지 않습니다.

pdh6941's profile image
pdh6941

asked

Ask a question