問題一覧 > 通常問題

No.697 How Many Ponds Are There?

レベル : / 実行時間制限 : 1ケース 6.000秒 / メモリ制限 : 300 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 168
作問者 : yuki2006
お気に入りにしたユーザー ProblemId : 1543 / 自分の提出
問題文最終更新日: 2022-01-21 16:16:48
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original

Problem Statement

You are given an $H \times W$ field. The field contains only $0$ and $1$.
$0$ is a ground cell and $1$ is a water cell.
A single pond is what is formed by joining $1$s that are connected vertically or horizontally.
Diagonal cells are not connected.
Even a single cell counts as a pond.

How many ponds are there in this field?
Everything outside the field is considered ground.

(The time limit is generous, but be careful about the number of function calls and the memory limit.)
This may be tough for LL languages. It may be adjusted at a later date.

Input

$H\ W$
$A_{1,1} \dots A_{W,1}$
$\dots$
$A_{1,H} \dots A_{W,H}$

$1\le H,W \le 3000$
$A_{i,j}$ is $0$ or $1$

Output

Output the number of ponds.

Sample

Sample 1
Input
4 3
1 0 1
0 1 0
1 0 1
0 1 0
Output
6

There are 6 pieces of water consisting of 1 cell each.
Even a single cell is a pond, so there are 6 ponds.

Sample 2
Input
4 3
1 1 1
0 1 0
1 0 1
1 1 1
Output
2

The water cells join together to form 2 ponds.

Sample 3
Input
3 4
0 0 0 0
0 0 0 0
0 0 0 0
Output
0

There may be no water at all.

Sample 4
Input
3 4
1 1 1 1
1 1 1 1
1 1 1 1
Output
1

Everything is water and forms one pond.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。