結果
問題 | No.34 砂漠の行商人 |
ユーザー |
![]() |
提出日時 | 2020-09-05 20:42:08 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 885 bytes |
コンパイル時間 | 402 ms |
コンパイル使用メモリ | 82,152 KB |
実行使用メモリ | 409,088 KB |
最終ジャッジ日時 | 2024-11-29 06:07:03 |
合計ジャッジ時間 | 18,472 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 24 TLE * 2 |
ソースコード
from heapq import heappush, heappop n, v, sy, sx, gy, gx = map(int, input().split()) l = [list(map(int, input().split())) for _ in range(n)] sx -= 1 sy -= 1 gx -= 1 gy -= 1 shortest_dist = abs(sx - gx) + abs(sy - gy) if v > shortest_dist * 9: print(shortest_dist) exit(0) INF = 10 ** 4 dist = [[[INF for _ in range(v + 1)] for _ in range(n)] for _ in range(n)] dist[sx][sy][v] = 0 q = [(0, sx, sy, v)] while q: d, x, y, vit = heappop(q) if d > dist[x][y][vit]: continue for dx, dy in (1, 0), (0, 1), (-1, 0), (0, -1): nx, ny = x + dx, y + dy if not (0 <= nx < n and 0 <= ny < n): continue nvit = vit - l[nx][ny] nd = d + 1 if nvit > 0 and nd< dist[nx][ny][nvit]: dist[nx][ny][nvit] = nd heappush(q, (nd, nx, ny, nvit)) ans = min(dist[gx][gy]) if ans == INF: ans = -1 print(ans)