結果

問題 No.971 いたずらっ子
ユーザー tamato
提出日時 2020-01-17 21:36:01
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,217 ms / 2,000 ms
コード長 958 bytes
コンパイル時間 228 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 180,736 KB
最終ジャッジ日時 2024-11-07 11:24:59
合計ジャッジ時間 11,354 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    import sys
    from collections import deque
    input = sys.stdin.readline

    H, W = map(int, input().split())
    grid = []
    for _ in range(H):
        line = input().rstrip('\n')
        grid.append(line)

    d = [(0, 1), (1, 0), (-1, 0), (0, -1)]
    inf = float('inf')
    que = deque()
    que.append((0, 0))
    seen = [[inf] * W for _ in range(H)]
    seen[0][0] = 0
    while que:
        h, w = que.popleft()
        for dh, dw in d:
            h_new, w_new = h+dh, w+dw
            if 0 <= h_new < H and 0 <= w_new < W:
                if seen[h_new][w_new] > 10**9:
                    que.append((h_new, w_new))
                if grid[h_new][w_new] == '.':
                    seen[h_new][w_new] = min(seen[h_new][w_new], seen[h][w] + 1)
                else:
                    seen[h_new][w_new] = min(seen[h_new][w_new], seen[h][w] + 1 + h_new + w_new)
    print(seen[-1][-1])


if __name__ == '__main__':
    main()
0