結果

問題 No.157 2つの空洞
ユーザー hiragnhiragn
提出日時 2022-12-21 12:48:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 36 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 98 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-29 03:14:44
合計ジャッジ時間 1,684 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

from itertools import product


def main():
    w, h = map(int, input().split())
    g = [list(input()) for _ in range(h)]

    def dfs(x, y):
        g[x][y] = '#'
        yield [x, y]

        dx = [1, 0, -1, 0, 1, -1, -1, 1]
        dy = [0, 1, 0, -1, 1, 1, -1, -1]
        for dx, dy in zip(dx[:4], dy[:4]):
            nx = x + dx
            ny = y + dy
            if not (0 <= nx < h and 0 <= ny < w):
                continue
            if g[nx][ny] == '.':
                yield from dfs(nx, ny)

    cave1, cave2 = [], []
    is_first = True
    for i, j in product(range(h), range(w)):
        if g[i][j] == '.':
            if is_first:
                cave1 = list(dfs(i, j))
                is_first = False
            else:
                cave2 = list(dfs(i, j))
                break

    ans = 1 << 60
    for x1, y1 in cave1:
        for x2, y2 in cave2:
            ans = min(ans, abs(x1 - x2) + abs(y1 - y2) - 1)
    print(ans)


if __name__ == "__main__":
    main()
0