結果

問題 No.34 砂漠の行商人
ユーザー kept1994kept1994
提出日時 2022-05-07 17:33:50
言語 PyPy3
(7.3.13)
結果
TLE  
実行時間 -
コード長 1,441 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 86,924 KB
実行使用メモリ 432,336 KB
最終ジャッジ日時 2023-09-21 04:17:09
合計ジャッジ時間 18,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,692 KB
testcase_01 AC 106 ms
77,016 KB
testcase_02 AC 119 ms
77,804 KB
testcase_03 AC 99 ms
73,132 KB
testcase_04 AC 213 ms
116,000 KB
testcase_05 AC 217 ms
128,168 KB
testcase_06 AC 139 ms
99,216 KB
testcase_07 AC 272 ms
153,196 KB
testcase_08 AC 349 ms
181,956 KB
testcase_09 AC 2,459 ms
301,260 KB
testcase_10 AC 423 ms
227,016 KB
testcase_11 TLE -
testcase_12 AC 327 ms
117,284 KB
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

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)
    Grid = [list(map(int, input().split())) for _ in range(N)]

    def bfs(G, H, W, startY, startX, goalY, goalX, 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()
            if nowY == goalY and nowX == goalX:
                return dist[nowY][nowX][now_power]
            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 -1
    
    print(bfs(Grid, N, N, Sy - 1, Sx - 1, Gy - 1, Gx - 1, V))
    # ans = min(dist[Gy - 1][Gx - 1][1:])
    # print(ans if ans != INF else -1)
    return

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