結果

問題 No.34 砂漠の行商人
ユーザー kept1994kept1994
提出日時 2022-05-07 17:42:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,084 ms / 5,000 ms
コード長 1,365 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 87,272 KB
実行使用メモリ 428,388 KB
最終ジャッジ日時 2023-09-21 04:26:47
合計ジャッジ時間 9,926 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,552 KB
testcase_01 AC 102 ms
77,000 KB
testcase_02 AC 120 ms
77,784 KB
testcase_03 AC 96 ms
71,348 KB
testcase_04 AC 208 ms
116,124 KB
testcase_05 AC 215 ms
128,308 KB
testcase_06 AC 142 ms
99,248 KB
testcase_07 AC 278 ms
153,772 KB
testcase_08 AC 343 ms
183,192 KB
testcase_09 AC 100 ms
77,000 KB
testcase_10 AC 101 ms
77,072 KB
testcase_11 AC 102 ms
76,644 KB
testcase_12 AC 187 ms
117,152 KB
testcase_13 AC 104 ms
76,968 KB
testcase_14 AC 103 ms
76,920 KB
testcase_15 AC 101 ms
77,068 KB
testcase_16 AC 102 ms
76,932 KB
testcase_17 AC 103 ms
77,072 KB
testcase_18 AC 96 ms
71,508 KB
testcase_19 AC 1,787 ms
373,724 KB
testcase_20 AC 3,084 ms
428,388 KB
testcase_21 AC 247 ms
212,892 KB
testcase_22 AC 105 ms
76,592 KB
testcase_23 AC 102 ms
76,736 KB
testcase_24 AC 105 ms
76,936 KB
testcase_25 AC 322 ms
169,460 KB
権限があれば一括ダウンロードができます

ソースコード

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