結果

問題 No.13 囲みたい!
ユーザー takakintakakin
提出日時 2020-05-24 15:58:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,581 ms / 5,000 ms
コード長 917 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 102,816 KB
最終ジャッジ日時 2024-04-20 01:11:01
合計ジャッジ時間 3,235 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
52,480 KB
testcase_01 AC 35 ms
52,812 KB
testcase_02 AC 34 ms
54,156 KB
testcase_03 AC 56 ms
68,436 KB
testcase_04 AC 55 ms
69,108 KB
testcase_05 AC 1,581 ms
102,816 KB
testcase_06 AC 59 ms
69,188 KB
testcase_07 AC 245 ms
80,280 KB
testcase_08 AC 44 ms
65,116 KB
testcase_09 AC 45 ms
64,324 KB
testcase_10 AC 44 ms
62,000 KB
testcase_11 AC 46 ms
64,044 KB
testcase_12 AC 41 ms
60,316 KB
testcase_13 AC 47 ms
62,380 KB
testcase_14 AC 46 ms
63,148 KB
testcase_15 AC 36 ms
53,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
w,h=map(int,input().split())
n=w*h
edge=[[] for _ in range(n)]
M=[[int(i) for i in input().split()] for _ in range(h)]
d=((1,0),(0,1),(-1,0),(0,-1))
E=[0]*1001
for i in range(n):
  ix,iy=i//w,i%w
  for dx,dy in d:
    if 0<=ix+dx<h and 0<=iy+dy<w:
      if M[ix][iy]==M[ix+dx][iy+dy]:
        edge[i].append((ix+dx)*w+(iy+dy))
        E[M[ix][iy]]+=1
ans=False
for i in range(n):
  ix,iy=i//w,i%w
  if E[M[ix][iy]]<8 or len(edge[i])<2:
    continue
  else:
    Chk=[False]*n
    Prev=[None]*n
    Chk[i]=True
    S=[]
    S.append(i)
    while S:
      s=S.pop()
      for g in edge[s]:
        if Chk[g] and g!=Prev[s]:
          ans=True
          break
        elif not Chk[g]:
          Prev[g]=s
          S.append(g)
          Chk[g]=True
        else:
          continue
  if ans==True:
    break
if ans:
  print("possible")
else:
  print("impossible")
  
0