結果

問題 No.402 最も海から遠い場所
ユーザー rin204
提出日時 2022-02-02 15:55:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,198 ms / 3,000 ms
コード長 785 bytes
コンパイル時間 391 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 452,096 KB
最終ジャッジ日時 2024-06-11 09:28:08
合計ジャッジ時間 12,565 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

h, w = map(int, input().split())
S = ["." * (w + 2)] + ["." + input() + "." for _ in range(h)] + ["." * (w + 2)]
h += 2
w += 2
queue = deque()
dist = [[-1] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if S[i][j] == ".":
            dist[i][j] = 0
            queue.append((i, j))

ans = 0
while queue:
    i, j = queue.popleft()
    for di in range(-1, 2):
        ni = i + di
        if ni == -1 or ni == h:
            continue
        for dj in range(-1, 2):
            nj = j + dj
            if nj == -1 or nj == w:
                continue
            if dist[ni][nj] != -1:
                continue
            dist[ni][nj] = dist[i][j] + 1
            ans = dist[ni][nj]
            queue.append((ni, nj))

print(ans)
0