結果

問題 No.157 2つの空洞
ユーザー 👑 rin204rin204
提出日時 2022-01-20 09:23:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 106 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 77,100 KB
最終ジャッジ日時 2023-08-15 08:33:12
合計ジャッジ時間 3,560 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
71,452 KB
testcase_01 AC 94 ms
71,408 KB
testcase_02 AC 92 ms
71,452 KB
testcase_03 AC 93 ms
71,096 KB
testcase_04 AC 95 ms
71,268 KB
testcase_05 AC 93 ms
70,900 KB
testcase_06 AC 94 ms
70,912 KB
testcase_07 AC 94 ms
71,400 KB
testcase_08 AC 95 ms
71,328 KB
testcase_09 AC 95 ms
71,448 KB
testcase_10 AC 94 ms
71,332 KB
testcase_11 AC 95 ms
71,268 KB
testcase_12 AC 95 ms
71,316 KB
testcase_13 AC 96 ms
71,492 KB
testcase_14 AC 97 ms
71,352 KB
testcase_15 AC 104 ms
76,848 KB
testcase_16 AC 103 ms
76,864 KB
testcase_17 AC 106 ms
77,100 KB
testcase_18 AC 96 ms
71,260 KB
testcase_19 AC 103 ms
76,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

w, h = map(int, input().split())
C = [input() for _ in range(h)]
br = False
stack = []
queue = deque()
dist = [[-1] * w for _ in range(h)]
for i in range(h):
    for j in range(w):
        if C[i][j] == ".":
            stack.append((i, j))
            queue.append((i, j))
            dist[i][j] = 0
            br = True
            break
    if br:
        break

directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
while stack:
    i, j = stack.pop()
    for di, dj in directions:
        ni = i + di
        nj = j + dj
        if ni == -1 or nj == -1 or ni == h or nj == w:
            continue
        if dist[ni][nj] != -1 or C[ni][nj] == "#":
            continue
        dist[ni][nj] = 0
        stack.append((ni, nj))
        queue.append((ni, nj))
        
while queue:
    i, j = queue.popleft()
    for di, dj in directions:
        ni = i + di
        nj = j + dj
        if ni == -1 or nj == -1 or ni == h or nj == w:
            continue
        if dist[ni][nj] != -1:
            continue
        if C[ni][nj] == ".":
            print(dist[i][j])
            queue.clear()
            break
        dist[ni][nj] = dist[i][j] + 1
        queue.append((ni, nj))

0