結果

問題 No.971 いたずらっ子
ユーザー ttr
提出日時 2020-01-29 20:32:01
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,225 bytes
コンパイル時間 197 ms
コンパイル使用メモリ 82,164 KB
実行使用メモリ 168,796 KB
最終ジャッジ日時 2024-09-16 02:10:16
合計ジャッジ時間 6,871 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
H,W = map(int, input().split())
L = [input() for _ in range(H)]

inf = 10**15
dist = [[inf]*W for _ in range(H)]
h = []
heapq.heappush(h, (0, 0, 0, 0))
while dist[H-1][W-1] == inf:
    temp = heapq.heappop(h)
    d = temp[0]
    i = temp[1]
    j = temp[2]
    sub_d = temp[3]
    if dist[i][j] < inf:
        continue
    dist[i][j] = d
    if i > 0 and dist[i-1][j] == inf:
        if L[i-1][j] == ".":
            heapq.heappush(h, (d+1, i-1, j, sub_d+1))
        elif L[i-1][j] == "k":
            heapq.heappush(h, (d+2+sub_d, i-1, j, sub_d+1))
    if i < H-1 and dist[i+1][j] == inf:
        if L[i+1][j] == ".":
            heapq.heappush(h, (d+1, i+1, j, sub_d+1))
        elif L[i+1][j] == "k":
            heapq.heappush(h, (d+2+sub_d, i+1, j, sub_d+1))
    if j > 0 and dist[i][j-1] == inf:
        if L[i][j-1] == ".":
            heapq.heappush(h, (d+1, i, j-1, sub_d+1))
        elif L[i][j-1] == "k":
            heapq.heappush(h, (d+2+sub_d, i, j-1, sub_d+1))
    if j < W-1 and dist[i][j+1] == inf:
        if L[i][j+1] == ".":
            heapq.heappush(h, (d+1, i, j+1, sub_d+1))
        elif L[i][j+1] == "k":
            heapq.heappush(h, (d+2+sub_d, i, j+1, sub_d+1))

print(dist[H-1][W-1])
0