結果
| 問題 | No.34 砂漠の行商人 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-12-12 21:37:14 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2,875 ms / 5,000 ms | 
| コード長 | 888 bytes | 
| コンパイル時間 | 212 ms | 
| コンパイル使用メモリ | 82,048 KB | 
| 実行使用メモリ | 361,520 KB | 
| 最終ジャッジ日時 | 2024-06-28 10:38:24 | 
| 合計ジャッジ時間 | 8,334 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 26 | 
ソースコード
from queue import Queue
from collections import deque
def solve():
  n, v, sy, sx, gy, gx = list(map(int, input().split()))
  if v > (abs(sx - gx) + abs(sy - gy)) * 9:
    return abs(sx - gx) + abs(sy - gy)
  gx, gy = gx - 1, gy - 1
  g = [list(map(int, input().split())) for i in range(n)]
  dis = [[[-1] * (v + 1) for j in range(n)] for i in range(n)]
  dis[sx - 1][sy - 1][v] = 0
  queue = deque()
  queue.append((sx - 1, sy - 1, v))
  dxy = [(0, 1), (0, -1), (1, 0), (-1, 0)]
  while queue:
    u = queue.popleft()
    cost = dis[u[0]][u[1]][u[2]] + 1
    for d in dxy:
      x, y = u[0] + d[0], u[1] + d[1]
      if x < 0 or x >= n or y < 0 or y >= n:
        continue
      z = u[2] - g[x][y]
      if z > 0 and dis[x][y][z] == -1:
        dis[x][y][z] = cost
        if x == gx and y == gy:
          return dis[x][y][z]
        queue.append((x, y, z))
  return -1
print(solve())
            
            
            
        