結果

問題 No.34 砂漠の行商人
ユーザー kept1994
提出日時 2022-05-07 17:42:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,648 ms / 5,000 ms
コード長 1,365 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 82,128 KB
実行使用メモリ 428,192 KB
最終ジャッジ日時 2024-07-06 23:03:11
合計ジャッジ時間 7,476 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #

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

input = sys.stdin.readline

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

def main():
    N, V, Sx, Sy, Gx, Gy = map(int, input().split())
    power_init = min(V, powerMax)
    G = [list(map(int, input().split())) for _ in range(N)]
    if V > (abs(Sx-Gx) + abs(Sy-Gy)) * 9:
        print(abs(Sx-Gx) + abs(Sy-Gy))
        return
    
    startY = Sy - 1
    startX = Sx - 1
    goalY = Gy - 1
    goalX = Gx - 1
    q = deque()
    dist = [[[INF] * (powerMax + 1) for _ in range(N)] for _ in range(N)]
    q.append((0, startY, startX, power_init))
    dist[startY][startX][power_init] = 0
    while q:
        min_cost, nowY, nowX, now_power = q.popleft()
        if nowY == goalY and nowX == goalX:
            print(min_cost)
            return
        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 >= N or nextx >= N:
                continue
            next_power = now_power - G[nexty][nextx]
            if next_power <= 0 or dist[nexty][nextx][next_power] != INF:
                continue
            q.append((min_cost + 1, nexty, nextx, next_power))
            dist[nexty][nextx][next_power] = min_cost + 1
    print(-1)
    return

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