結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-07-25 23:04:52 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 3,841 ms / 5,000 ms | 
| コード長 | 904 bytes | 
| コンパイル時間 | 333 ms | 
| コンパイル使用メモリ | 82,420 KB | 
| 実行使用メモリ | 300,460 KB | 
| 最終ジャッジ日時 | 2024-06-27 18:15:58 | 
| 合計ジャッジ時間 | 16,317 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
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)
            
            
            
        