結果

問題 No.157 2つの空洞
ユーザー 👑 rin204rin204
提出日時 2022-01-20 09:23:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 1,207 bytes
コンパイル時間 181 ms
コンパイル使用メモリ 82,688 KB
実行使用メモリ 62,976 KB
最終ジャッジ日時 2024-05-02 20:15:56
合計ジャッジ時間 2,218 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,504 KB
testcase_01 AC 48 ms
53,760 KB
testcase_02 AC 46 ms
53,504 KB
testcase_03 AC 47 ms
53,888 KB
testcase_04 AC 48 ms
54,016 KB
testcase_05 AC 48 ms
54,016 KB
testcase_06 AC 47 ms
54,016 KB
testcase_07 AC 47 ms
53,760 KB
testcase_08 AC 50 ms
53,888 KB
testcase_09 AC 48 ms
53,888 KB
testcase_10 AC 48 ms
53,888 KB
testcase_11 AC 49 ms
54,272 KB
testcase_12 AC 52 ms
54,016 KB
testcase_13 AC 49 ms
54,144 KB
testcase_14 AC 50 ms
54,400 KB
testcase_15 AC 57 ms
61,440 KB
testcase_16 AC 55 ms
60,544 KB
testcase_17 AC 59 ms
62,976 KB
testcase_18 AC 48 ms
54,016 KB
testcase_19 AC 54 ms
60,672 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