結果

問題 No.697 池の数はいくつか
ユーザー 双六双六
提出日時 2020-08-26 15:14:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,308 ms / 6,000 ms
コード長 1,159 bytes
コンパイル時間 392 ms
コンパイル使用メモリ 86,980 KB
実行使用メモリ 292,052 KB
最終ジャッジ日時 2023-08-16 23:40:24
合計ジャッジ時間 21,255 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,788 KB
testcase_01 AC 93 ms
71,764 KB
testcase_02 AC 98 ms
71,440 KB
testcase_03 AC 95 ms
71,792 KB
testcase_04 AC 94 ms
71,584 KB
testcase_05 AC 93 ms
71,792 KB
testcase_06 AC 94 ms
71,756 KB
testcase_07 AC 94 ms
71,576 KB
testcase_08 AC 94 ms
71,424 KB
testcase_09 AC 93 ms
71,420 KB
testcase_10 AC 93 ms
71,424 KB
testcase_11 AC 94 ms
71,892 KB
testcase_12 AC 93 ms
71,520 KB
testcase_13 AC 97 ms
71,552 KB
testcase_14 AC 95 ms
71,428 KB
testcase_15 AC 95 ms
71,456 KB
testcase_16 AC 95 ms
71,448 KB
testcase_17 AC 96 ms
71,424 KB
testcase_18 AC 98 ms
71,884 KB
testcase_19 AC 97 ms
71,924 KB
testcase_20 AC 96 ms
71,788 KB
testcase_21 AC 95 ms
71,628 KB
testcase_22 AC 94 ms
71,704 KB
testcase_23 AC 92 ms
71,796 KB
testcase_24 AC 553 ms
99,772 KB
testcase_25 AC 559 ms
100,268 KB
testcase_26 AC 562 ms
100,324 KB
testcase_27 AC 545 ms
99,480 KB
testcase_28 AC 578 ms
100,060 KB
testcase_29 AC 1,931 ms
292,052 KB
testcase_30 AC 2,166 ms
249,776 KB
testcase_31 AC 1,912 ms
291,724 KB
testcase_32 AC 2,276 ms
249,744 KB
testcase_33 AC 2,308 ms
250,600 KB
testcase_34 AC 2,109 ms
250,092 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