結果

問題 No.2328 Build Walls
ユーザー kemunikukemuniku
提出日時 2023-05-28 15:06:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,178 ms / 3,000 ms
コード長 1,105 bytes
コンパイル時間 381 ms
コンパイル使用メモリ 86,764 KB
実行使用メモリ 95,972 KB
最終ジャッジ日時 2023-08-27 11:25:17
合計ジャッジ時間 14,351 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,324 KB
testcase_01 AC 75 ms
71,196 KB
testcase_02 AC 76 ms
71,284 KB
testcase_03 AC 76 ms
71,336 KB
testcase_04 AC 76 ms
71,396 KB
testcase_05 AC 75 ms
71,232 KB
testcase_06 AC 75 ms
71,124 KB
testcase_07 AC 76 ms
71,452 KB
testcase_08 AC 77 ms
71,340 KB
testcase_09 AC 75 ms
71,340 KB
testcase_10 AC 76 ms
71,200 KB
testcase_11 AC 77 ms
70,968 KB
testcase_12 AC 77 ms
71,344 KB
testcase_13 AC 168 ms
83,404 KB
testcase_14 AC 404 ms
82,332 KB
testcase_15 AC 315 ms
81,544 KB
testcase_16 AC 151 ms
78,396 KB
testcase_17 AC 290 ms
82,008 KB
testcase_18 AC 121 ms
78,032 KB
testcase_19 AC 90 ms
75,628 KB
testcase_20 AC 135 ms
78,284 KB
testcase_21 AC 120 ms
79,564 KB
testcase_22 AC 590 ms
84,800 KB
testcase_23 AC 899 ms
95,940 KB
testcase_24 AC 843 ms
95,300 KB
testcase_25 AC 890 ms
95,676 KB
testcase_26 AC 654 ms
94,188 KB
testcase_27 AC 798 ms
94,504 KB
testcase_28 AC 140 ms
81,368 KB
testcase_29 AC 914 ms
95,972 KB
testcase_30 AC 203 ms
92,140 KB
testcase_31 AC 189 ms
88,720 KB
testcase_32 AC 765 ms
94,696 KB
testcase_33 AC 1,178 ms
94,932 KB
testcase_34 AC 216 ms
92,920 KB
testcase_35 AC 1,013 ms
94,220 KB
testcase_36 AC 77 ms
71,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#壁の置き方を経路と考える。
#8方向すべてに行くことができる
#左端から右端に到達出来たら可能
#すべてのHについて探索する。DFSするのでH*H*W
#800**3って通るんすか?通らなそう
#ダイクストラ法みたいなことをすればよい?
import heapq
H,W = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H-2)]
queue = []
costs = [[float('inf')]*W for i in range(H-2)]
for i in range(H-2):
    if A[i][0] != -1:
        costs[i][0] = A[i][0]
        heapq.heappush(queue,(A[i][0],i,0))
while queue:
    cost,h,w = heapq.heappop(queue)
    if cost > costs[h][w]:
        continue
    for dh,dw in ((0,1),(1,0),(-1,0),(0,-1),(1,1),(-1,1),(1,-1),(-1,-1)):
        if 0 <= h+dh < H-2 and 0 <= w+dw < W and A[h+dh][w+dw] != -1:
            temp = costs[h][w] + A[h+dh][w+dw]
            if temp < costs[h+dh][w+dw]:
                costs[h+dh][w+dw] = temp
                heapq.heappush(queue,(temp,h+dh,w+dw))
ans = min([costs[i][-1] for i in range(H-2)])
if ans == float("inf"):
    print(-1)
else:
    print(ans)
0