結果

問題 No.34 砂漠の行商人
ユーザー roarisroaris
提出日時 2020-07-25 22:56:38
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,234 bytes
コンパイル時間 424 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 834,716 KB
最終ジャッジ日時 2023-09-10 02:06:01
合計ジャッジ時間 9,257 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,440 KB
testcase_01 AC 107 ms
77,536 KB
testcase_02 AC 125 ms
78,256 KB
testcase_03 AC 144 ms
82,480 KB
testcase_04 AC 307 ms
122,840 KB
testcase_05 AC 334 ms
132,952 KB
testcase_06 AC 130 ms
78,560 KB
testcase_07 AC 432 ms
161,608 KB
testcase_08 AC 724 ms
219,472 KB
testcase_09 AC 101 ms
76,944 KB
testcase_10 AC 98 ms
76,792 KB
testcase_11 AC 99 ms
76,960 KB
testcase_12 AC 300 ms
120,864 KB
testcase_13 WA -
testcase_14 AC 100 ms
77,020 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 MLE -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque

def bfs():
    dist = [-1]*c
    dist[serial[Sx][Sy][V]] = 0
    q = deque([serial[Sx][Sy][V]])
    
    while q:
        v = q.popleft()
        
        for nv in G[v]:
            if dist[nv]==-1:
                dist[nv] = dist[v]+1
                q.append(nv)
    
    return dist

N, V, Sy, Sx, Gy, Gx = map(int, input().split())
Sy -= 1
Sx -= 1
Gy -= 1
Gx -= 1
L = [list(map(int, input().split())) for _ in range(N)]

if V>=1800:
    print(abs(Sx-Sy)+abs(Gx-Gy))
    exit()

serial = [[[-1]*(V+1) for _ in range(N)] for _ in range(N)]
c = 0

for i in range(N):
    for j in range(N):
        for k in range(V+1):
            serial[i][j][k] = c
            c += 1

G = [[] for _ in range(c)]

for i in range(N):
    for j in range(N):
        for k in range(V+1):
            for ni, nj in [(i-1, j), (i+1, j), (i, j-1), (i, j+1)]:
                if 0<=ni<N and 0<=nj<N and k-L[ni][nj]>0:
                    G[serial[i][j][k]].append(serial[ni][nj][k-L[ni][nj]])

dist = bfs()
ans = 10**18

for i in range(V+1):
    if dist[serial[Gx][Gy][i]]!=-1:
        ans = min(ans, dist[serial[Gx][Gy][i]])
        
if ans==10**18:
    print(-1)
else:
    print(ans)
0