結果
| 問題 |
No.157 2つの空洞
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-06-14 00:04:00 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 53 ms / 2,000 ms |
| コード長 | 1,138 bytes |
| コンパイル時間 | 308 ms |
| コンパイル使用メモリ | 82,268 KB |
| 実行使用メモリ | 65,728 KB |
| 最終ジャッジ日時 | 2024-06-22 11:06:40 |
| 合計ジャッジ時間 | 2,080 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / 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))