結果
| 問題 |
No.2328 Build Walls
|
| コンテスト | |
| ユーザー |
navel_tos
|
| 提出日時 | 2023-05-29 19:35:54 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 1,220 ms / 3,000 ms |
| コード長 | 978 bytes |
| コンパイル時間 | 309 ms |
| コンパイル使用メモリ | 81,792 KB |
| 実行使用メモリ | 90,816 KB |
| 最終ジャッジ日時 | 2024-12-28 10:15:39 |
| 合計ジャッジ時間 | 13,357 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 34 |
ソースコード
#MMA Contest 015 G
'''
DPみを感じる。こちらのほうが解きやすい・・・のか?
単に状態をもったDPをしたらTLEするに決まっている。
大胆予想: 左から右に王将の要領で移動する。右端にたどり着く最小コストを求めよ。
'''
import heapq as hq
import sys
f=lambda:list(map(int,input().split()))
H,W=f(); A=[[-1]*(W+1)]+[f()+[-1] for _ in range(H-2)]+[[-1]*(W+1)]
Q=[]; cost=[[10**9]*W for _ in range(H)]
for h in range(1,H-1): hq.heappush(Q,(A[h][0],h,0)) if A[h][0]>=0 else None
while Q:
dist,h,w=hq.heappop(Q)
if dist>cost[h][w]: continue
if w==W-1: print(dist); sys.exit()
cost[h][w]=dist
for x,y in [(-1,0),(1,0),(0,-1),(0,1),(-1,-1),(-1,1),(1,-1),(1,1)]:
if A[h+x][w+y]>=0 and cost[h+x][w+y]>dist+A[h+x][w+y]:
cost[h+x][w+y]=dist+A[h+x][w+y]; hq.heappush(Q,(dist+A[h+x][w+y],h+x,w+y))
ans=min(cost[h][-1] for h in range(H))
print(ans) if ans<10**9 else print(-1)
navel_tos