結果
問題 | No.34 砂漠の行商人 |
ユーザー |
|
提出日時 | 2016-09-03 22:14:20 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 336 ms / 5,000 ms |
コード長 | 1,216 bytes |
コンパイル時間 | 335 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 11,648 KB |
最終ジャッジ日時 | 2024-06-28 10:33:11 |
合計ジャッジ時間 | 3,892 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 26 |
ソースコード
import queue from collections import namedtuple dx = (1, 0, -1, 0) dy = (0, -1, 0, 1) Point = namedtuple('Point', 'x y') def solve(): n, v, sx, sy, gx, gy = [int(i) for i in input().split(' ')] start = Point(x = sx-1, y = sy-1) goal = Point(x = gx-1, y = gy-1) costs = [[int(i) for i in input().split(' ')] for _ in range(n)] mincosts = [[v for _ in range(n)] for _ in range(n)] mincosts[start.y][start.x] = 0 q = queue.Queue() q.put(start) cnt = 0 while True: tmp = queue.Queue() if q.empty() or mincosts[goal.y][goal.x] < v: break while not q.empty(): x, y = q.get() for tx, ty in zip(dx, dy): nx = x + tx ny = y + ty if nx >= 0 and nx < n and ny >= 0 and ny < n: tmpcost = mincosts[y][x] + costs[ny][nx] if mincosts[ny][nx] > tmpcost: mincosts[ny][nx] = tmpcost tmp.put(Point(nx, ny)) q = tmp cnt += 1 if mincosts[goal.y][goal.x] < v: print(cnt) else: print(-1) if __name__ == "__main__": solve()