結果

問題 No.157 2つの空洞
ユーザー yomo3yomo3
提出日時 2020-03-12 00:49:07
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 968 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 10,896 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-08-10 04:45:20
合計ジャッジ時間 2,151 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
8,696 KB
testcase_01 AC 19 ms
8,532 KB
testcase_02 AC 18 ms
8,540 KB
testcase_03 AC 19 ms
8,536 KB
testcase_04 AC 19 ms
8,648 KB
testcase_05 AC 19 ms
8,664 KB
testcase_06 AC 18 ms
8,660 KB
testcase_07 AC 19 ms
8,688 KB
testcase_08 AC 18 ms
8,588 KB
testcase_09 AC 18 ms
8,668 KB
testcase_10 AC 19 ms
8,656 KB
testcase_11 AC 19 ms
8,668 KB
testcase_12 AC 19 ms
8,476 KB
testcase_13 AC 19 ms
8,540 KB
testcase_14 AC 18 ms
8,492 KB
testcase_15 AC 21 ms
8,672 KB
testcase_16 AC 20 ms
8,696 KB
testcase_17 AC 20 ms
8,572 KB
testcase_18 AC 19 ms
8,484 KB
testcase_19 AC 19 ms
8,584 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