結果

問題 No.697 池の数はいくつか
ユーザー 双六
提出日時 2020-08-26 15:14:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,860 ms / 6,000 ms
コード長 1,159 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 292,952 KB
最終ジャッジ日時 2024-11-25 16:22:17
合計ジャッジ時間 16,297 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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