結果

問題 No.157 2つの空洞
ユーザー 12354865271235486527
提出日時 2019-12-14 04:42:08
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 93 ms
コンパイル使用メモリ 10,932 KB
実行使用メモリ 9,240 KB
最終ジャッジ日時 2023-09-10 11:28:26
合計ジャッジ時間 1,785 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 23 ms
9,220 KB
testcase_01 AC 24 ms
9,052 KB
testcase_02 AC 24 ms
9,120 KB
testcase_03 AC 25 ms
9,060 KB
testcase_04 AC 24 ms
9,132 KB
testcase_05 AC 24 ms
9,064 KB
testcase_06 AC 23 ms
9,020 KB
testcase_07 AC 23 ms
9,052 KB
testcase_08 AC 24 ms
9,036 KB
testcase_09 AC 24 ms
9,168 KB
testcase_10 AC 27 ms
9,132 KB
testcase_11 AC 24 ms
9,052 KB
testcase_12 AC 23 ms
9,000 KB
testcase_13 AC 24 ms
9,164 KB
testcase_14 AC 25 ms
9,036 KB
testcase_15 AC 25 ms
9,000 KB
testcase_16 AC 24 ms
9,136 KB
testcase_17 AC 31 ms
9,240 KB
testcase_18 AC 23 ms
9,136 KB
testcase_19 AC 23 ms
9,072 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from queue import Queue

def bfs(y, x, C, cave, num):
    Q = Queue()
    Q.put((y, x))
    while not Q.empty():
        n = Q.get()
        if C[n[0]][n[1]-1] == '.' and (n[0], n[1]-1) not in cave[num]:
            cave[num].append((n[0], n[1]-1))
            Q.put((n[0], n[1]-1))
        if C[n[0]][n[1]+1] == '.' and (n[0], n[1]+1) not in cave[num]:
            cave[num].append((n[0], n[1]+1))
            Q.put((n[0], n[1]+1))
        if C[n[0]-1][n[1]] == '.' and (n[0]-1, n[1]) not in cave[num]:
            cave[num].append((n[0]-1, n[1]))
            Q.put((n[0]-1, n[1]))
        if C[n[0]+1][n[1]] == '.' and (n[0]+1, n[1]) not in cave[num]:
            cave[num].append((n[0]+1, n[1]))
            Q.put((n[0]+1, n[1]))
    return

W, H = map(int, input().split())
C = []
for _ in range(H):
    C.append(input())
num = 0
cave = [[],[]]
for y in range(1, H-1):
    for x in range(1, W-1):
        if C[y][x] == '.' and (y, x) not in cave[0] and (y, x) not in cave[1]:
            cave[num].append((y, x))
            bfs(y, x, C, cave, num)
            num += 1
min_dist = float('inf')
for c1 in cave[0]:
    for c2 in cave[1]:
        min_dist = min(min_dist, abs(c1[0] - c2[0]) + abs(c1[1] - c2[1]) - 1)
print(min_dist)
0