結果

問題 No.157 2つの空洞
ユーザー tnodinotnodino
提出日時 2022-06-18 14:34:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 858 bytes
コンパイル時間 590 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-18 10:19:12
合計ジャッジ時間 1,654 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

from collections import deque
def bfs():
    for i in range(H):
        for j in range(W):
            if C[i][j] == '.':
                C[i][j] = '#'
                Queue = deque()
                Queue.append((i, j))
                s = [(i, j)]
                while Queue:
                    x,y = Queue.popleft()
                    for i in range(4):
                        nx,ny = x+dx[i],y+dy[i]
                        if C[nx][ny] == '.':
                            C[nx][ny] = '#'
                            Queue.append((nx, ny))
                            s.append((nx, ny))
                return s

dx = [1, -1, 0, 0]
dy = [0, 0, 1, -1]
W,H = map(int,input().split())
C = [list(input()) for _ in range(H)]
s = bfs()
t = bfs()
ans = 1<<16
for sx,sy in s:
    for tx,ty in t:
        ans = min(ans, abs(sx-tx) + abs(sy-ty) - 1)
print(ans)
0