結果

問題 No.34 砂漠の行商人
ユーザー zimphazimpha
提出日時 2017-12-12 21:37:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,038 ms / 5,000 ms
コード長 888 bytes
コンパイル時間 903 ms
コンパイル使用メモリ 87,284 KB
実行使用メモリ 362,012 KB
最終ジャッジ日時 2023-09-10 19:30:49
合計ジャッジ時間 12,200 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 173 ms
80,908 KB
testcase_01 AC 207 ms
82,036 KB
testcase_02 AC 198 ms
82,724 KB
testcase_03 AC 174 ms
80,804 KB
testcase_04 AC 249 ms
87,612 KB
testcase_05 AC 251 ms
88,080 KB
testcase_06 AC 201 ms
82,660 KB
testcase_07 AC 281 ms
91,288 KB
testcase_08 AC 330 ms
97,340 KB
testcase_09 AC 174 ms
81,000 KB
testcase_10 AC 171 ms
80,888 KB
testcase_11 AC 172 ms
80,792 KB
testcase_12 AC 234 ms
86,712 KB
testcase_13 AC 169 ms
80,812 KB
testcase_14 AC 171 ms
80,928 KB
testcase_15 AC 169 ms
80,692 KB
testcase_16 AC 173 ms
81,136 KB
testcase_17 AC 171 ms
80,732 KB
testcase_18 AC 172 ms
80,820 KB
testcase_19 AC 1,816 ms
289,964 KB
testcase_20 AC 3,038 ms
362,012 KB
testcase_21 AC 199 ms
82,788 KB
testcase_22 AC 171 ms
80,936 KB
testcase_23 AC 171 ms
80,908 KB
testcase_24 AC 174 ms
80,760 KB
testcase_25 AC 352 ms
103,160 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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())
0