結果

問題 No.697 池の数はいくつか
ユーザー 双六双六
提出日時 2020-08-26 15:13:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,774 ms / 6,000 ms
コード長 1,155 bytes
コンパイル時間 331 ms
コンパイル使用メモリ 82,736 KB
実行使用メモリ 288,424 KB
最終ジャッジ日時 2024-04-25 20:51:17
合計ジャッジ時間 15,620 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
54,368 KB
testcase_01 AC 36 ms
54,416 KB
testcase_02 AC 36 ms
55,304 KB
testcase_03 AC 36 ms
54,368 KB
testcase_04 AC 35 ms
54,212 KB
testcase_05 AC 37 ms
54,324 KB
testcase_06 AC 35 ms
54,924 KB
testcase_07 AC 37 ms
55,364 KB
testcase_08 AC 37 ms
55,916 KB
testcase_09 AC 38 ms
55,116 KB
testcase_10 AC 38 ms
54,796 KB
testcase_11 AC 38 ms
55,336 KB
testcase_12 AC 39 ms
55,000 KB
testcase_13 AC 39 ms
55,824 KB
testcase_14 AC 37 ms
55,460 KB
testcase_15 AC 37 ms
55,096 KB
testcase_16 AC 37 ms
53,860 KB
testcase_17 AC 37 ms
54,248 KB
testcase_18 AC 38 ms
54,696 KB
testcase_19 AC 38 ms
54,736 KB
testcase_20 AC 39 ms
54,696 KB
testcase_21 AC 37 ms
56,076 KB
testcase_22 AC 38 ms
55,068 KB
testcase_23 AC 38 ms
55,160 KB
testcase_24 AC 428 ms
97,692 KB
testcase_25 AC 417 ms
97,892 KB
testcase_26 AC 420 ms
97,716 KB
testcase_27 AC 414 ms
97,492 KB
testcase_28 AC 429 ms
97,780 KB
testcase_29 AC 1,511 ms
288,424 KB
testcase_30 AC 1,753 ms
246,868 KB
testcase_31 AC 1,530 ms
288,112 KB
testcase_32 AC 1,717 ms
246,712 KB
testcase_33 AC 1,774 ms
246,704 KB
testcase_34 AC 1,719 ms
246,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
from collections import deque
con = 10 ** 9 + 7; INF = float("inf")

def getlist():
	return list(map(int, input().split()))

def BFS(H, W, L, visit, start):
	h, w = start
	Q = deque(); Q.append([h, w])
	while Q:
		y, x = Q.popleft()
		if L[y - 1][x] == 1 and visit[y - 1][x] == 0:
			visit[y - 1][x] = 1
			Q.append([y - 1, x])
		if L[y + 1][x] == 1 and visit[y + 1][x] == 0:
			visit[y + 1][x] = 1
			Q.append([y + 1, x])
		if L[y][x - 1] == 1 and visit[y][x - 1] == 0:
			visit[y][x - 1] = 1
			Q.append([y, x - 1])
		if L[y][x + 1] == 1 and visit[y][x + 1] == 0:
			visit[y][x + 1] = 1
			Q.append([y, x + 1])

#処理内容
def main():
	H, W = getlist()
	L = []
	L.append([0] * (W + 2))
	for i in range(H):
		l = getlist()
		L.append([0] + l + [0])
	L.append([0] * (W + 2))

	visit = [[0] * (W + 2) for i in range(H + 2)]
	# print(L)
	cnt = 0
	for i in range(1, H + 1):
		for j in range(1, W + 1):
			if visit[i][j] == 0 and L[i][j] == 1:
				cnt += 1
				BFS(H + 2, W + 2, L, visit, [i, j])

	print(cnt)

if __name__ == '__main__':
	main()
0