結果

問題 No.157 2つの空洞
ユーザー yomo3yomo3
提出日時 2020-03-12 00:49:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 29 ms / 2,000 ms
コード長 968 bytes
コンパイル時間 96 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-27 22:05:43
合計ジャッジ時間 1,144 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
10,880 KB
testcase_01 AC 24 ms
10,752 KB
testcase_02 AC 25 ms
10,880 KB
testcase_03 AC 25 ms
11,008 KB
testcase_04 AC 25 ms
11,008 KB
testcase_05 AC 27 ms
10,880 KB
testcase_06 AC 29 ms
10,880 KB
testcase_07 AC 26 ms
10,752 KB
testcase_08 AC 25 ms
10,880 KB
testcase_09 AC 24 ms
10,880 KB
testcase_10 AC 24 ms
10,880 KB
testcase_11 AC 24 ms
10,752 KB
testcase_12 AC 23 ms
10,880 KB
testcase_13 AC 24 ms
10,880 KB
testcase_14 AC 26 ms
10,880 KB
testcase_15 AC 27 ms
10,880 KB
testcase_16 AC 24 ms
10,880 KB
testcase_17 AC 26 ms
10,880 KB
testcase_18 AC 25 ms
10,880 KB
testcase_19 AC 28 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

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

routes = []
for h in range(H-1):
    for w in range(W-1):
        if C[h][w] == '.':
            r = []
            q = deque()
            q.append((h, w))
            C[h][w] = '*' #done
            r.append((h, w))
            while q:
                y0, x0 = q.popleft()
                C[y0][x0] = '*' #done
                r.append((y0, x0))
                for dy, dx in ((-1, 0), (0, 1), (1, 0), (0, -1)):
                    y1, x1 = y0 + dy, x0 + dx
                    if 0 <= y1 < H and 0 <= x1 < W and C[y1][x1] == '.':
                        q.append((y1, x1))
                        C[y1][x1] = '*' #done
                        r.append((y1, x1))
            routes.append(r)

ans = H * W
for p0 in routes[0]:
    for p1 in routes[1]:
        d = abs(p0[0] - p1[0]) + abs(p0[1] - p1[1])
        if d - 1 < ans:
            ans = d - 1

print(ans)
0