結果

問題 No.157 2つの空洞
ユーザー H3PO4
提出日時 2020-05-07 01:11:03
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,142 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-07-03 09:14:02
合計ジャッジ時間 1,406 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
from collections import deque

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

table = [[None] * W for _ in range(H)]
for h, w in itertools.product(range(1, H - 1), range(1, W - 1)):
    if C[h][w] == '.':
        h0, w0 = h, w
        break

# bfs(連結部分)
D0 = deque([(h0, w0)])
D = deque()
table[h0][w0] = 0
while D0:
    h, w = D0.popleft()
    for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None:
            if C[h + dh][w + dw] == '.':
                D0.append((h + dh, w + dw))
                table[h + dh][w + dw] = 0
            else:
                D.append((h + dh, w + dw))
                table[h + dh][w + dw] = 1
# bfs(壁)
while D:
    h, w = D.popleft()
    for dh, dw in ((1, 0), (-1, 0), (0, 1), (0, -1)):
        if 0 <= h + dh < H and 0 <= w + dw < W and table[h + dh][w + dw] is None:
            D.append((h + dh, w + dw))
            if C[h + dh][w + dw] == '.':
                print(table[h][w])
                exit()
            else:
                table[h + dh][w + dw] = table[h][w] + 1
0