結果

問題 No.402 最も海から遠い場所
ユーザー mitsuo
提出日時 2018-08-24 10:56:01
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 1,196 bytes
コンパイル時間 217 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 18,176 KB
最終ジャッジ日時 2024-06-11 16:51:02
合計ジャッジ時間 6,691 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 7 WA * 3 TLE * 1 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def solve(H, W, M):
    dmap = []
    candidates = []

    for h in range(0, H):
        dmap.append([-1] * W)
        for w in range(0, W):
            if M[h][w] == ".":
                dmap[h][w] = 0
                heapq.heappush(candidates, (0, h, w))
            elif (h == 0 or h == H - 1 or w == 0 or w == W - 1) and M[h][w] == "#":
                dmap[h][w] = 1
                heapq.heappush(candidates, (1, h, w))

    max = 0
    while(len(candidates)):
        can = heapq.heappop(candidates)
        d = can[0]
        h = can[1]
        w = can[2]

        for (xi, yi) in [(h-1, w-1),(h-1, w), (h-1, w+1),
                     (h , w - 1), (h, w + 1),
                     (h + 1, w - 1), (h + 1, w), (h + 1, w + 1)]:
            if xi > 0 and yi > 0 and xi < H and yi < W:
                if dmap[xi][yi] == -1:
                    dmap[xi][yi] = d + 1
                    max = d + 1
                    if (d + 1, xi, yi) not in candidates:
                        heapq.heappush(candidates, (d + 1, xi, yi))
    return max

if __name__ == "__main__":
    H, W = tuple([int(c) for c in input().split(" ")])
    print(solve(H, W, [input() for _ in range(0, H)]))
0