結果

問題 No.13 囲みたい!
ユーザー TawaraTawara
提出日時 2016-07-27 23:52:13
言語 Python2
(2.7.18)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 657 bytes
コンパイル時間 466 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-24 10:26:20
合計ジャッジ時間 1,453 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 12 ms
6,656 KB
testcase_01 AC 11 ms
6,656 KB
testcase_02 AC 11 ms
6,656 KB
testcase_03 AC 25 ms
6,912 KB
testcase_04 AC 15 ms
6,784 KB
testcase_05 AC 35 ms
6,912 KB
testcase_06 AC 16 ms
6,912 KB
testcase_07 AC 37 ms
6,912 KB
testcase_08 AC 38 ms
6,944 KB
testcase_09 AC 36 ms
6,940 KB
testcase_10 AC 15 ms
6,784 KB
testcase_11 AC 31 ms
6,912 KB
testcase_12 AC 13 ms
6,528 KB
testcase_13 AC 20 ms
6,784 KB
testcase_14 AC 20 ms
6,784 KB
testcase_15 AC 11 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

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