結果
| 問題 | No.157 2つの空洞 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-14 00:04:00 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 57 ms / 2,000 ms |
| コード長 | 1,138 bytes |
| 記録 | |
| コンパイル時間 | 385 ms |
| コンパイル使用メモリ | 85,872 KB |
| 実行使用メモリ | 64,816 KB |
| 最終ジャッジ日時 | 2026-03-06 16:44:16 |
| 合計ジャッジ時間 | 1,773 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 16 |
ソースコード
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))