結果

問題 No.157 2つの空洞
コンテスト
ユーザー nebukuro09
提出日時 2016-10-06 14:19:51
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 104 ms / 2,000 ms
コード長 1,073 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 177 ms
コンパイル使用メモリ 77,300 KB
最終ジャッジ日時 2025-12-03 21:54:57
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
k1 = set()
W, H = map(int, raw_input().split())
board = [raw_input() for _ in xrange(H)]
drdc = [(1, 0), (-1, 0), (0, -1), (0, 1)]

def bfs(starts, collect):
    q = deque()
    for s in starts:
        q.append((s[0], s[1], 0))
    visited = set()
    while len(q) > 0:
        r, c, depth = q.popleft()
        if (r, c) in visited:
            continue
        if (not collect) and depth > 0 and board[r][c] == '.':
            return depth
        visited.add((r, c))
        for dr, dc in drdc:
            nr, nc, nd = r+dr, c+dc, depth
            if nr < 0 or nr >= H or nc < 0 or nc >= W:
                continue
            if (nr, nc) in visited:
                continue
            if board[nr][nc] == '#':
                if collect:
                    continue
                else:
                    nd += 1
            q.append((nr, nc, nd))
    return visited

for i, j in [(x, y) for x in xrange(H) for y in xrange(W)]:
    if board[i][j] == '.':
        k1 = bfs([(i, j)], True)
        break
print bfs(list(k1), False)
0