結果

問題 No.13 囲みたい!
ユーザー HydruHydru
提出日時 2023-01-22 19:43:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 123 ms / 5,000 ms
コード長 1,431 bytes
コンパイル時間 1,355 ms
コンパイル使用メモリ 86,884 KB
実行使用メモリ 78,540 KB
最終ジャッジ日時 2023-09-07 04:26:24
合計ジャッジ時間 3,482 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,632 KB
testcase_01 AC 92 ms
71,640 KB
testcase_02 AC 93 ms
71,648 KB
testcase_03 AC 112 ms
77,804 KB
testcase_04 AC 99 ms
76,816 KB
testcase_05 AC 121 ms
78,396 KB
testcase_06 AC 102 ms
76,844 KB
testcase_07 AC 123 ms
78,540 KB
testcase_08 AC 110 ms
77,728 KB
testcase_09 AC 110 ms
77,700 KB
testcase_10 AC 105 ms
77,736 KB
testcase_11 AC 110 ms
77,764 KB
testcase_12 AC 98 ms
72,364 KB
testcase_13 AC 116 ms
77,604 KB
testcase_14 AC 110 ms
77,788 KB
testcase_15 AC 93 ms
71,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

w,h=map(int,input().split())

M=[list(map(int,input().split())) for _ in range(h)]

visit=[[0 for _ in range(w)] for _ in range(h)]

for i in range(h):
    for j in range(w):
        if visit[i][j]:
            continue
        Q=deque()
        visit[i][j]=1
        Q.append((i,j))
        while(Q):
            y,x=Q.pop()
            cnt=0
            if y!=0:
                if M[y-1][x]==M[y][x]:
                    if visit[y-1][x]:
                        cnt+=1
                    else:
                        visit[y-1][x]=1
                        Q.append((y-1,x))
            if y!=h-1:
                if M[y+1][x]==M[y][x]:
                    if visit[y+1][x]:
                        cnt+=1
                    else:
                        visit[y+1][x]=1
                        Q.append((y+1,x))
            if x!=0:
                if M[y][x-1]==M[y][x]:
                    if visit[y][x-1]:
                        cnt+=1
                    else:
                        visit[y][x-1]=1
                        Q.append((y,x-1))
            if x!=w-1:
                if M[y][x+1]==M[y][x]:
                    if visit[y][x+1]:
                        cnt+=1
                    else:
                        visit[y][x+1]=1
                        Q.append((y,x+1))
            if cnt>=2:
                print("possible")
                exit()
    
print("impossible")
0