結果

問題 No.13 囲みたい!
ユーザー nsd_fbnsd_fb
提出日時 2015-02-19 07:50:26
言語 Python2
(2.7.18)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 1,080 bytes
コンパイル時間 78 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-04-21 12:16:45
合計ジャッジ時間 1,344 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
6,656 KB
testcase_01 AC 13 ms
6,656 KB
testcase_02 AC 13 ms
6,528 KB
testcase_03 AC 23 ms
6,912 KB
testcase_04 AC 16 ms
6,784 KB
testcase_05 AC 31 ms
6,912 KB
testcase_06 AC 18 ms
6,912 KB
testcase_07 AC 31 ms
6,912 KB
testcase_08 AC 33 ms
7,040 KB
testcase_09 AC 32 ms
6,912 KB
testcase_10 AC 16 ms
6,656 KB
testcase_11 AC 32 ms
6,784 KB
testcase_12 AC 14 ms
6,528 KB
testcase_13 AC 19 ms
6,528 KB
testcase_14 AC 19 ms
6,784 KB
testcase_15 AC 13 ms
6,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections

directions = ((-1, 0), (0, -1), (1, 0), (0, 1))

def bfs(sx, sy, w, h, field, visited):
    queue = collections.deque()
    queue.append((sx, sy, -1, -1))
    visited[sy][sx] = True

    while queue:
        x, y, px, py = queue.popleft()

        for dx, dy in directions:
            nx = x + dx
            ny = y + dy

            if (not (0 <= nx < w and 0 <= ny < h) or
                nx == px and ny == py or
                field[y][x] != field[ny][nx]):
                continue

            if visited[ny][nx]:
                return True

            visited[ny][nx] = True
            queue.append((nx, ny, x, y))

    return False

def solve():
    w, h = map(int, raw_input().split())
    field = [map(int, raw_input().split()) for _ in xrange(h)]
    visited = [[False] * w for _ in xrange(h)]

    for y in xrange(h):
        for x in xrange(w):
            if visited[y][x]:
                continue

            if bfs(x, y, w, h, field, visited):
                return True

    return False

print 'possible' if solve() else 'impossible'
0