結果

問題 No.157 2つの空洞
ユーザー H3PO4H3PO4
提出日時 2020-05-07 01:11:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,142 bytes
コンパイル時間 81 ms
コンパイル使用メモリ 10,776 KB
実行使用メモリ 8,756 KB
最終ジャッジ日時 2023-09-16 07:38:19
合計ジャッジ時間 1,573 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,576 KB
testcase_01 AC 19 ms
8,688 KB
testcase_02 AC 22 ms
8,636 KB
testcase_03 AC 21 ms
8,752 KB
testcase_04 AC 20 ms
8,756 KB
testcase_05 AC 19 ms
8,620 KB
testcase_06 AC 19 ms
8,652 KB
testcase_07 AC 18 ms
8,500 KB
testcase_08 AC 18 ms
8,584 KB
testcase_09 AC 19 ms
8,576 KB
testcase_10 AC 19 ms
8,620 KB
testcase_11 AC 19 ms
8,572 KB
testcase_12 AC 20 ms
8,536 KB
testcase_13 AC 20 ms
8,632 KB
testcase_14 AC 19 ms
8,588 KB
testcase_15 AC 20 ms
8,584 KB
testcase_16 AC 21 ms
8,660 KB
testcase_17 AC 20 ms
8,600 KB
testcase_18 AC 19 ms
8,568 KB
testcase_19 AC 20 ms
8,576 KB
権限があれば一括ダウンロードができます

ソースコード

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