結果

問題 No.13 囲みたい!
ユーザー ckawatakckawatak
提出日時 2019-04-16 22:19:31
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,455 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 81,656 KB
実行使用メモリ 77,992 KB
最終ジャッジ日時 2023-10-22 07:14:17
合計ジャッジ時間 10,013 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 42 ms
55,432 KB
testcase_02 AC 42 ms
55,432 KB
testcase_03 AC 68 ms
72,528 KB
testcase_04 AC 74 ms
76,080 KB
testcase_05 AC 4,872 ms
77,992 KB
testcase_06 WA -
testcase_07 AC 740 ms
77,384 KB
testcase_08 AC 620 ms
76,404 KB
testcase_09 AC 617 ms
76,404 KB
testcase_10 AC 97 ms
76,180 KB
testcase_11 AC 394 ms
76,416 KB
testcase_12 AC 73 ms
75,544 KB
testcase_13 AC 137 ms
76,276 KB
testcase_14 AC 146 ms
76,252 KB
testcase_15 AC 50 ms
63,704 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):
    number = M[y][x]
    que = deque()
    que.appendleft((y,x))
    while len(que) != 0:
        h,w = que.popleft()
        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 \
               visited[nh][nw] == False and M[nh][nw] == number:
                que.appendleft((nh,nw))
    return found()

def found():
    for h in range(H):
        for w in range(W):
            if visited[h][w]:
                count = 0
                for i in range(len(dh)):
                    nh = h + dh[i]
                    nw = w + dw[i]
                    if 0 <= nh < H and 0 <= nw < W and visited[nh][nw]:
                        count = count + 1
                if count < 2:
                    return False
    return True

def reset():
    for h in range(H):
        for w in range(W):
            visited[h][w] = 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