結果

問題 No.13 囲みたい!
ユーザー gigurururugigurururu
提出日時 2014-11-28 17:36:45
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 50 ms / 5,000 ms
コード長 572 bytes
コンパイル時間 123 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 12,288 KB
最終ジャッジ日時 2024-04-21 12:15:16
合計ジャッジ時間 1,355 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
10,752 KB
testcase_01 AC 26 ms
10,752 KB
testcase_02 AC 25 ms
10,752 KB
testcase_03 AC 47 ms
12,288 KB
testcase_04 AC 42 ms
11,904 KB
testcase_05 AC 49 ms
12,160 KB
testcase_06 AC 50 ms
12,032 KB
testcase_07 AC 48 ms
12,032 KB
testcase_08 AC 50 ms
12,288 KB
testcase_09 AC 50 ms
12,160 KB
testcase_10 AC 32 ms
11,008 KB
testcase_11 AC 43 ms
11,904 KB
testcase_12 AC 26 ms
10,880 KB
testcase_13 AC 33 ms
11,136 KB
testcase_14 AC 34 ms
11,264 KB
testcase_15 AC 25 ms
10,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

w,h=map(int,input().split())
m=[[int(i) for i in input().split()]+[0] for _ in range(h)]+[[0]*w]

checked=set()

def dfs(x,y):
  if (x,y) in checked: return False
  r=[(0,-1),(1,0),(-1,0),(0,1)]
  c=m[y][x]
  stack=[(-1,x,y)]
  while len(stack):
    dir,tx,ty=stack.pop()
    if m[ty][tx]==c:
      if (tx,ty) in checked: return True
      checked.add((tx,ty))
      for i,(dx,dy) in enumerate(r):
        if i!=dir:
          stack.append((3-i,tx+dx,ty+dy))

print( "possible" if any([dfs(x,y) for x in range(w) for y in range(h)]) else "impossible")
0