結果

問題 No.13 囲みたい!
ユーザー nebukuro09nebukuro09
提出日時 2016-09-21 14:51:14
言語 Python2
(2.7.18)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 895 bytes
コンパイル時間 634 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 8,320 KB
最終ジャッジ日時 2024-04-28 16:44:46
合計ジャッジ時間 1,146 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
6,144 KB
testcase_01 AC 11 ms
6,144 KB
testcase_02 AC 11 ms
6,144 KB
testcase_03 AC 27 ms
6,940 KB
testcase_04 AC 40 ms
8,320 KB
testcase_05 AC 40 ms
7,680 KB
testcase_06 AC 17 ms
6,400 KB
testcase_07 AC 41 ms
7,552 KB
testcase_08 AC 43 ms
7,680 KB
testcase_09 AC 42 ms
7,552 KB
testcase_10 AC 16 ms
6,656 KB
testcase_11 AC 36 ms
7,680 KB
testcase_12 AC 13 ms
6,272 KB
testcase_13 AC 21 ms
6,656 KB
testcase_14 AC 21 ms
6,784 KB
testcase_15 AC 11 ms
6,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

W, H = map(int, raw_input().split())
board = [map(int, raw_input().split()) for _ in xrange(H)]
visited = set()

dr = [0, 0, 1, -1]
dc = [1, -1, 0, 0]

def dfs(start):
    stack = [start]
    while len(stack) > 0:
        row, col = stack.pop()
        if (row, col) in visited:
            return True
        visited.add((row, col))
        for i in xrange(4):
            nrow = row+dr[i]
            ncol = col+dc[i]
            if nrow < 0 or nrow >= H or ncol < 0 or ncol >= W:
                continue
            if (nrow, ncol) in visited:
                continue
            if board[nrow][ncol] != board[row][col]:
                continue
            stack.append((nrow, ncol))
    return False

for i in xrange(H):
    for j in xrange(W):
        if (i, j) in visited:
            continue
        if dfs((i, j)):
            print 'possible'
            exit()
print 'impossible'
0