結果

問題 No.157 2つの空洞
ユーザー ryusukeryusuke
提出日時 2022-07-26 12:28:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 1,107 bytes
コンパイル時間 1,559 ms
コンパイル使用メモリ 86,680 KB
実行使用メモリ 76,716 KB
最終ジャッジ日時 2023-09-23 05:17:28
合計ジャッジ時間 3,350 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,124 KB
testcase_01 AC 98 ms
71,580 KB
testcase_02 AC 99 ms
71,524 KB
testcase_03 AC 98 ms
71,412 KB
testcase_04 AC 96 ms
71,400 KB
testcase_05 AC 97 ms
71,312 KB
testcase_06 AC 97 ms
71,408 KB
testcase_07 AC 97 ms
71,420 KB
testcase_08 AC 97 ms
71,344 KB
testcase_09 AC 97 ms
71,420 KB
testcase_10 AC 98 ms
71,552 KB
testcase_11 AC 96 ms
71,472 KB
testcase_12 AC 97 ms
71,448 KB
testcase_13 AC 98 ms
71,428 KB
testcase_14 AC 98 ms
71,576 KB
testcase_15 AC 105 ms
76,492 KB
testcase_16 AC 96 ms
71,532 KB
testcase_17 AC 106 ms
76,716 KB
testcase_18 AC 98 ms
71,492 KB
testcase_19 AC 98 ms
71,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def bfs(sy: int, sx: int, num: int) -> None:
    dist[sy][sx] = num
    q = deque()
    q.append((sy, sx))
    while q:
        vy, vx = q.popleft()
        for dy, dx in ((1, 0), (-1, 0), (0, 1), (0, -1)):
            y = vy + dy
            x = vx + dx
            if not (0 <= x < w and 0 <= y < h):
                continue
            if grid[y][x] == "#":
                continue
            if dist[y][x] != -1:
                continue
            dist[y][x] = dist[vy][vx]
            q.append((y, x))

w, h = map(int, input().split())
grid = [list(input()) for _ in range(h)]
from collections import deque
dist = [[-1] * w for _ in range(h)]
cnt = 1
for i in range(h):
    for j in range(w):
        if grid[i][j] == "." and dist[i][j] == -1:
            bfs(i, j, 1000 * cnt)
            cnt += 1

a = []
b = []
for i in range(h):
    for j in range(w):
        if dist[i][j] == 1000:
            a.append((i, j))
        if dist[i][j] == 2000:
            b.append((i, j))

ans = 10 ** 18
for xi, yi in a:
    for xj, yj in b:
        ans = min(ans, abs(xi - xj) + abs(yi - yj))

print(ans - 1)
0