結果

問題 No.157 2つの空洞
ユーザー lloyzlloyz
提出日時 2023-06-14 00:04:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 111 ms / 2,000 ms
コード長 1,138 bytes
コンパイル時間 1,174 ms
コンパイル使用メモリ 86,704 KB
実行使用メモリ 77,496 KB
最終ジャッジ日時 2023-09-04 12:26:25
合計ジャッジ時間 3,576 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
71,584 KB
testcase_01 AC 98 ms
71,696 KB
testcase_02 AC 98 ms
71,384 KB
testcase_03 AC 97 ms
71,252 KB
testcase_04 AC 97 ms
71,448 KB
testcase_05 AC 98 ms
71,556 KB
testcase_06 AC 98 ms
71,660 KB
testcase_07 AC 99 ms
71,636 KB
testcase_08 AC 97 ms
71,572 KB
testcase_09 AC 98 ms
71,588 KB
testcase_10 AC 98 ms
71,296 KB
testcase_11 AC 99 ms
71,432 KB
testcase_12 AC 100 ms
71,468 KB
testcase_13 AC 99 ms
71,416 KB
testcase_14 AC 107 ms
76,896 KB
testcase_15 AC 110 ms
77,176 KB
testcase_16 AC 110 ms
77,496 KB
testcase_17 AC 111 ms
77,072 KB
testcase_18 AC 98 ms
71,708 KB
testcase_19 AC 108 ms
77,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

w, h = map(int, input().split())
S = [list(input()) for _ in range(h)]

ci, cj = None, None
for i in range(h):
    for j in range(w):
        if S[i][j] == '.':
            ci, cj = i, j
            break
    if ci is not None:
        break

Directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
Que = deque([(ci, cj)])
start_set = set()
start_set.add((ci, cj))
while Que:
    ci, cj = Que.popleft()
    for di, dj in Directions:
        ni, nj = ci + di, cj + dj
        if 0 <= ni < h and 0 <= nj < w:
            if S[ni][nj] == '.' and (ni, nj) not in start_set:
                start_set.add((ni, nj))
                Que.append((ni, nj))

Que = deque()
for ci, cj in start_set:
    Que.append((ci, cj, 0))
seen = set()
while Que:
    ci, cj, cnt = Que.popleft()
    if S[ci][cj] == '.' and (ci, cj) not in start_set:
        print(cnt - 1)
        break
    for di, dj in Directions:
        ni, nj = ci + di, cj + dj
        if 0 <= ni < h and 0 <= nj < w:
            if (ni, nj) not in start_set and (ni, nj) not in seen:
                seen.add((ni, nj))
                Que.append((ni, nj, cnt + 1))
0