結果

問題 No.971 いたずらっ子
ユーザー tktk_snsn
提出日時 2020-12-25 17:31:47
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 946 bytes
コンパイル時間 386 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 150,012 KB
最終ジャッジ日時 2024-09-22 10:37:20
合計ジャッジ時間 7,973 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 4
other TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
inf = 10**18
DX = (-1, 0, 1, 0, -1, -1, 1, 1)
DY = (0, 1, 0, -1, -1, 1, -1, 1)
DX = DX[:4]
DY = DY[:4]


H, W = map(int, input().split())
G = [input().rstrip() for _ in range(H)]
dist = [[inf] * W for _ in range(H)]
dist[0][0] = 0
que = [(0, 0, 0)]
while que:
    ds, x, y = heapq.heappop(que)
    if dist[x][y] < ds:
        continue
    d = ds + 1
    for dx, dy in zip(DX, DY):
        nx = x + dx
        ny = y + dy
        dk = ds + 1 + nx + ny
        if 0 <= nx < H and 0 <= ny < W:
            if G[nx][ny] == "." and dist[nx][ny] > d:
                dist[nx][ny] = d
                heapq.heappush(que, (d, nx, ny))
            elif G[nx][ny] == "k" and dist[nx][ny] > dk:
                dist[nx][ny] = dk
                heapq.heappush(que, (dk, nx, ny))

# if True:
#     import numpy as np
#     print(np.array(dist)[:10, :10])
print(dist[H-1][W-1])
0