結果
問題 |
No.34 砂漠の行商人
|
ユーザー |
|
提出日時 | 2016-09-03 22:28:43 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 280 ms / 5,000 ms |
コード長 | 1,455 bytes |
コンパイル時間 | 103 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 11,776 KB |
最終ジャッジ日時 | 2024-06-28 10:33:15 |
合計ジャッジ時間 | 3,395 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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() in_queue = [[False for _ in range(n)] for _ in range(n)] q.put(start) in_queue[start.y][start.x] = True 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() in_queue[y][x] = False 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 if not in_queue[ny][nx]: tmp.put(Point(nx, ny)) in_queue[ny][nx] = True q = tmp cnt += 1 if mincosts[goal.y][goal.x] < v: print(cnt) else: print(-1) if __name__ == "__main__": solve()