結果

問題 No.34 砂漠の行商人
ユーザー kept1994
提出日時 2022-05-07 17:45:20
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,405 bytes
コンパイル時間 169 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 439,568 KB
最終ジャッジ日時 2024-07-06 23:05:48
合計ジャッジ時間 12,728 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 13 TLE * 1 -- * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3
import sys
from collections import deque
MOD = 998244353

powerMax = 99 * 2 * 9
INF = 10 ** 16

def main():
    N, V, Sx, Sy, Gx, Gy = map(int, input().split())
    V = min(V, powerMax)
    if V > (abs(Sx-Gx) + abs(Sy-Gy)) * 9:
        print(abs(Sx-Gx) + abs(Sy-Gy))
        return
    Grid = [list(map(int, input().split())) for _ in range(N)]

    def bfs(G, H, W, startY, startX, power_init) -> list:
        q = deque()
        dist = [[[INF] * (powerMax + 1) for _ in range(W)] for _ in range(H)]
        q.append((startY, startX, power_init))
        dist[startY][startX][power_init] = 0
        while q:
            nowy, nowx, now_power = q.popleft()
            for dx, dy in [(0, 1), (1, 0), (-1, 0), (0, -1)]:
                nexty = nowy + dy
                nextx = nowx + dx
                if nexty < 0 or nextx < 0 or nexty >= H or nextx >= W:
                    continue
                next_power = now_power - G[nexty][nextx]
                if next_power < 0 or dist[nexty][nextx][next_power] != INF:
                    continue
                q.append((nexty, nextx, next_power))
                dist[nexty][nextx][next_power] = dist[nowy][nowx][now_power] + 1
        return dist
    
    dist = bfs(Grid, N, N, Sy - 1, Sx - 1, V)
    ans = min(dist[Gy - 1][Gx - 1][1:])
    print(ans if ans != INF else -1)
    return

if __name__ == '__main__':
    main()
0