結果

問題 No.157 2つの空洞
ユーザー nebukuro09nebukuro09
提出日時 2016-10-06 14:19:51
言語 Python2
(2.7.18)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,073 bytes
コンパイル時間 667 ms
コンパイル使用メモリ 6,712 KB
実行使用メモリ 6,472 KB
最終ジャッジ日時 2023-08-13 23:59:58
合計ジャッジ時間 1,677 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
6,276 KB
testcase_01 AC 13 ms
6,296 KB
testcase_02 AC 13 ms
6,364 KB
testcase_03 AC 13 ms
6,212 KB
testcase_04 AC 13 ms
6,316 KB
testcase_05 AC 14 ms
6,284 KB
testcase_06 AC 14 ms
6,396 KB
testcase_07 AC 14 ms
6,276 KB
testcase_08 AC 13 ms
6,212 KB
testcase_09 AC 13 ms
6,296 KB
testcase_10 AC 13 ms
6,472 KB
testcase_11 AC 14 ms
6,336 KB
testcase_12 AC 13 ms
6,424 KB
testcase_13 AC 14 ms
6,424 KB
testcase_14 AC 14 ms
6,348 KB
testcase_15 AC 14 ms
6,308 KB
testcase_16 AC 14 ms
6,304 KB
testcase_17 AC 15 ms
6,420 KB
testcase_18 AC 14 ms
6,348 KB
testcase_19 AC 14 ms
6,316 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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