結果

問題 No.13 囲みたい!
ユーザー ckawatakckawatak
提出日時 2019-04-16 23:20:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,079 ms / 5,000 ms
コード長 1,233 bytes
コンパイル時間 164 ms
コンパイル使用メモリ 81,652 KB
実行使用メモリ 77,560 KB
最終ジャッジ日時 2023-10-22 07:15:05
合計ジャッジ時間 6,542 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
55,432 KB
testcase_01 AC 45 ms
55,432 KB
testcase_02 AC 42 ms
55,432 KB
testcase_03 AC 64 ms
70,420 KB
testcase_04 AC 75 ms
76,164 KB
testcase_05 AC 3,079 ms
77,464 KB
testcase_06 AC 60 ms
66,160 KB
testcase_07 AC 613 ms
77,560 KB
testcase_08 AC 445 ms
76,392 KB
testcase_09 AC 444 ms
76,392 KB
testcase_10 AC 84 ms
76,104 KB
testcase_11 AC 290 ms
76,356 KB
testcase_12 AC 60 ms
70,048 KB
testcase_13 AC 115 ms
76,332 KB
testcase_14 AC 119 ms
76,276 KB
testcase_15 AC 48 ms
61,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

W,H = list(map(int, input().split()))
M = []
for _ in range(H):
    M.append(list(map(int, input().split())))
    
visited = []
for _ in range(H):
    visited.append([False for _ in range(W)])

dh = [ 0, -1, 0, 1]
dw = [-1,  0, 1, 0]
    
def search(y,x):
    found = False
    number = M[y][x]
    que = deque()
    que.appendleft( ((-1,-1),(y,x)) )
    while len(que) != 0:
        back,now = que.popleft()
        u,v = back
        h,w = now
        visited[h][w] = True
        for i in range(len(dh)):
            nh = h + dh[i]
            nw = w + dw[i]
            if 0 <= nh < H and 0 <= nw < W and \
               (nh != u or nw != v) and M[nh][nw] == number:
                if visited[nh][nw]:
                    found = True
                    break
                else:
                    que.appendleft( ((h,w), (nh,nw)) )
    return found

def reset():
    for i in range(H):
        for j in range(W):
            visited[i][j] = False

possible = False
for i in range(H):
    for j in range(W):
        if search(i,j):
            possible = True
            break
        reset()
    if possible:
        break

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