結果

問題 No.157 2つの空洞
ユーザー hiragnhiragn
提出日時 2022-12-21 12:48:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 991 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 10,988 KB
実行使用メモリ 8,580 KB
最終ジャッジ日時 2023-08-11 10:45:31
合計ジャッジ時間 2,091 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,320 KB
testcase_01 AC 17 ms
8,260 KB
testcase_02 AC 17 ms
8,364 KB
testcase_03 AC 15 ms
8,392 KB
testcase_04 AC 16 ms
8,184 KB
testcase_05 AC 16 ms
8,296 KB
testcase_06 AC 16 ms
8,252 KB
testcase_07 AC 15 ms
8,252 KB
testcase_08 AC 16 ms
8,172 KB
testcase_09 AC 16 ms
8,324 KB
testcase_10 AC 16 ms
8,236 KB
testcase_11 AC 16 ms
8,256 KB
testcase_12 AC 16 ms
8,392 KB
testcase_13 AC 15 ms
8,280 KB
testcase_14 AC 16 ms
8,372 KB
testcase_15 AC 17 ms
8,236 KB
testcase_16 AC 15 ms
8,276 KB
testcase_17 AC 20 ms
8,580 KB
testcase_18 AC 16 ms
8,328 KB
testcase_19 AC 15 ms
8,320 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