結果

問題 No.34 砂漠の行商人
ユーザー roarisroaris
提出日時 2020-07-25 23:04:52
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 4,003 ms / 5,000 ms
コード長 904 bytes
コンパイル時間 285 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 301,088 KB
最終ジャッジ日時 2023-09-10 02:09:23
合計ジャッジ時間 18,135 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,776 KB
testcase_01 AC 99 ms
76,836 KB
testcase_02 AC 111 ms
77,404 KB
testcase_03 AC 120 ms
77,580 KB
testcase_04 AC 169 ms
82,988 KB
testcase_05 AC 164 ms
83,704 KB
testcase_06 AC 114 ms
77,820 KB
testcase_07 AC 197 ms
87,652 KB
testcase_08 AC 236 ms
93,796 KB
testcase_09 AC 99 ms
76,848 KB
testcase_10 AC 98 ms
77,008 KB
testcase_11 AC 100 ms
76,792 KB
testcase_12 AC 161 ms
83,524 KB
testcase_13 AC 98 ms
76,896 KB
testcase_14 AC 99 ms
76,932 KB
testcase_15 AC 97 ms
76,876 KB
testcase_16 AC 99 ms
76,780 KB
testcase_17 AC 3,578 ms
289,376 KB
testcase_18 AC 96 ms
71,584 KB
testcase_19 AC 1,918 ms
290,648 KB
testcase_20 AC 3,771 ms
301,088 KB
testcase_21 AC 117 ms
77,904 KB
testcase_22 AC 4,003 ms
300,872 KB
testcase_23 AC 100 ms
76,800 KB
testcase_24 AC 100 ms
77,072 KB
testcase_25 AC 316 ms
107,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def bfs():
    dist = [[[-1]*(V+1) for _ in range(N)] for _ in range(N)]
    dist[Sx][Sy][V] = 0
    q = deque([(Sx, Sy, V)])
    
    while q:
        x, y, v = q.popleft()
        
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if 0<=nx<N and 0<=ny<N and v-L[nx][ny]>0 and dist[nx][ny][v-L[nx][ny]]==-1:
                dist[nx][ny][v-L[nx][ny]] = dist[x][y][v]+1
                q.append((nx, ny, v-L[nx][ny]))
    
    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-Gx)+abs(Sy-Gy))
    exit()

dist = bfs()
ans = 10**18

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