5014-開始リンク


📚 5014-開始リンク


開始リンク
 

理解する


この問題は典型的な幅優先探索問題である.
全長、開始点、到達点、および増加および減少が発生した場合は、幅優先ナビゲーションを使用します.
ただ、私もこれが間違っています.
10 10 10 1 2 => 0

10 1 10 0 1 => use the stairs
use the stairsを無条件に出力すると、このような状況は存在しません!
上記の場合も人数をチェックします.
 

ソース

from collections import deque
import sys

read = sys.stdin.readline

f, s, g, u, d = map(int, read().split())

visited = [False] * (f + 1)

ud = [u, d * -1]


def bfs():
    q = deque()
    q.append((s, 0))
    global result

    visited[s] = True
    while q:
        cur_data, cnt = q.popleft()

        if cur_data == g:
            result = cnt
            return True

        for i in ud:
            next_layer = cur_data + i

            if 1 <= next_layer <= f and not visited[next_layer]:
                visited[next_layer] = True
                q.append((next_layer, cnt + 1))

    return False


result = 0

if bfs():
    print(result)
else:
    print("use the stairs")
 
採点結果