結果
| 問題 |
No.2328 Build Walls
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-05-28 15:09:48 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,555 ms / 3,000 ms |
| コード長 | 729 bytes |
| コンパイル時間 | 262 ms |
| コンパイル使用メモリ | 82,188 KB |
| 実行使用メモリ | 127,744 KB |
| 最終ジャッジ日時 | 2024-12-27 04:56:21 |
| 合計ジャッジ時間 | 14,764 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
from collections import deque
h, w = map(int, input().split())
h -= 2
a = [list(map(int, input().split())) + [-1] for _ in range(h)] + [[-1] * (w + 1)]
INF = 10**18
dp = [[INF] * (w + 1) for _ in range(h)] + [[INF] * (w + 1)]
que = deque()
for i in range(h):
if a[i][0] == -1:
continue
dp[i][0] = a[i][0]
que.append((i, 0))
while que:
x, y = que.popleft()
for i in range(-1, 2):
for j in range(-1, 2):
sx = x + i
sy = y + j
if a[sx][sy] != -1 and dp[sx][sy] > dp[x][y] + a[sx][sy]:
dp[sx][sy] = dp[x][y] + a[sx][sy]
que.append((sx, sy))
ans = min(v[-2] for v in dp)
print(ans if ans < INF else -1)
# print(*dp, sep="\n")