結果

問題 No.13 囲みたい!
ユーザー Tawara
提出日時 2016-07-27 23:52:13
言語 Python2
(2.7.18)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 657 bytes
コンパイル時間 760 ms
コンパイル使用メモリ 6,912 KB
実行使用メモリ 7,040 KB
最終ジャッジ日時 2024-11-06 18:02:39
合計ジャッジ時間 1,871 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 16
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
dxy = ((1,0),(0,1),(-1,0),(0,-1))
W,H = map(int,raw_input().split())
M = [map(int,raw_input().split()) for i in xrange(H)]
Gone = [[False]*W for i in xrange(H)] 
def bfs(h,num):
	if Gone[h[1]][h[0]]: return False
	Q = deque()
	Q.append(((-1,-1),h))
	while Q:
		b,h = Q.popleft()
		if Gone[h[1]][h[0]]: return True
		Gone[h[1]][h[0]] = True
		for dx,dy in dxy:
			nxt = (h[0] + dx, h[1] + dy)
			if nxt != b and 0 <= nxt[0] < W and 0 <= nxt[1] < H and M[nxt[1]][nxt[0]] == num:
				Q.append((h,nxt))
	return False
for i in xrange(H):
	for j in xrange(W):
		if bfs((j,i),M[i][j]):
			print "possible"
			quit()
print "impossible"
0