結果

問題 No.13 囲みたい!
ユーザー ayaoniayaoni
提出日時 2020-12-21 04:29:02
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 92 ms / 5,000 ms
コード長 1,268 bytes
コンパイル時間 1,352 ms
コンパイル使用メモリ 81,108 KB
実行使用メモリ 76,584 KB
最終ジャッジ日時 2023-10-21 11:27:29
合計ジャッジ時間 2,175 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,300 KB
testcase_01 AC 42 ms
55,300 KB
testcase_02 AC 43 ms
55,300 KB
testcase_03 AC 59 ms
66,152 KB
testcase_04 AC 49 ms
61,212 KB
testcase_05 AC 76 ms
76,000 KB
testcase_06 AC 60 ms
68,240 KB
testcase_07 AC 92 ms
76,584 KB
testcase_08 AC 72 ms
75,944 KB
testcase_09 AC 71 ms
75,948 KB
testcase_10 AC 62 ms
68,364 KB
testcase_11 AC 62 ms
73,748 KB
testcase_12 AC 56 ms
66,132 KB
testcase_13 AC 60 ms
68,316 KB
testcase_14 AC 65 ms
72,460 KB
testcase_15 AC 43 ms
55,300 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


W,H = MI()
M = [LI() for _ in range(H)]

flag = [[0]*W for _ in range(H)]
directions = [(-1,0),(1,0),(0,-1),(0,1)]
for i in range(H):
    for j in range(W):
        if flag[i][j] == 0:
            flag[i][j] = 1
            deq = deque([(i,j,-1,-1)])
            while deq:
                ci,cj,pi,pj = deq.pop()
                for di,dj in directions:
                    ni,nj = ci+di,cj+dj
                    if not (0 <= ni < H and 0 <= nj < W) or M[ni][nj] != M[i][j] or (ni,nj) == (pi,pj):
                        continue
                    elif flag[ni][nj] == 0:
                        deq.append((ni,nj,ci,cj))
                        flag[ni][nj] = 1
                    else:
                        print('possible')
                        exit()

print('impossible')
0