結果

問題 No.34 砂漠の行商人
ユーザー H3PO4H3PO4
提出日時 2020-04-21 09:25:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 233 ms / 5,000 ms
コード長 742 bytes
コンパイル時間 121 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-04-16 16:35:23
合計ジャッジ時間 3,214 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,624 KB
testcase_01 AC 31 ms
10,752 KB
testcase_02 AC 34 ms
10,880 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 61 ms
10,624 KB
testcase_05 AC 65 ms
10,624 KB
testcase_06 AC 49 ms
10,880 KB
testcase_07 AC 114 ms
10,880 KB
testcase_08 AC 127 ms
10,752 KB
testcase_09 AC 109 ms
10,880 KB
testcase_10 AC 54 ms
10,880 KB
testcase_11 AC 79 ms
11,264 KB
testcase_12 AC 41 ms
10,752 KB
testcase_13 AC 233 ms
11,136 KB
testcase_14 AC 232 ms
11,008 KB
testcase_15 AC 33 ms
10,880 KB
testcase_16 AC 52 ms
10,880 KB
testcase_17 AC 33 ms
10,880 KB
testcase_18 AC 34 ms
10,624 KB
testcase_19 AC 118 ms
10,880 KB
testcase_20 AC 164 ms
10,880 KB
testcase_21 AC 36 ms
10,752 KB
testcase_22 AC 39 ms
10,752 KB
testcase_23 AC 33 ms
10,880 KB
testcase_24 AC 190 ms
10,880 KB
testcase_25 AC 50 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
from sys import exit

N, V, Sx, Sy, Gx, Gy = map(int, input().split())
Sx, Sy, Gx, Gy = map(lambda x: x - 1, (Sx, Sy, Gx, Gy))
L = [tuple(map(int, input().split())) for _ in range(N)]
cost_table = [[V] * N for _ in range(N)]
cost_table[Sy][Sx] = 0

d = deque([(Sy, Sx, 0)])
while d:
    y, x, t = d.popleft()
    for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        if 0 <= y + dy < N and 0 <= x + dx < N and cost_table[y + dy][x + dx] > cost_table[y][x] + L[y + dy][x + dx]:
            d.append((y + dy, x + dx, t + 1))
            cost_table[y + dy][x + dx] = cost_table[y][x] + L[y + dy][x + dx]
            if (x + dx, y + dy) == (Gx, Gy):
                print(t + 1)
                exit()
print(-1)
0