結果

問題 No.697 池の数はいくつか
ユーザー wunderkammer2wunderkammer2
提出日時 2020-02-26 21:26:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,901 ms / 6,000 ms
コード長 1,317 bytes
コンパイル時間 1,129 ms
コンパイル使用メモリ 103,660 KB
実行使用メモリ 38,912 KB
最終ジャッジ日時 2024-04-25 20:43:49
合計ジャッジ時間 15,051 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 1 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 1 ms
5,376 KB
testcase_24 AC 181 ms
7,680 KB
testcase_25 AC 182 ms
7,552 KB
testcase_26 AC 180 ms
7,552 KB
testcase_27 AC 185 ms
7,552 KB
testcase_28 AC 178 ms
7,552 KB
testcase_29 AC 1,899 ms
38,912 KB
testcase_30 AC 1,833 ms
38,528 KB
testcase_31 AC 1,901 ms
38,912 KB
testcase_32 AC 1,837 ms
38,528 KB
testcase_33 AC 1,848 ms
38,656 KB
testcase_34 AC 1,818 ms
38,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<algorithm>
#include<cmath>
#include<cstdio>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<string>
#include<utility>
#include<vector>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
#define rep(i,n) for(int i=0;i<n;i++)
#define repl(i,s,e) for(int i=s;i<e;i++)
#define reple(i,s,e) for(int i=s;i<=e;i++)
#define revrep(i,n) for(int i=n-1;i>=0;i--)
#define all(x) (x).begin(),(x).end()

int H, W;

bool IsInRange(int w, int h)
{
	return 0 <= w && w <= W - 1 && 0 <= h && h <= H - 1;
}

int main()
{	
	cin >> H >> W;

	vector<vector<int>> A(W, vector<int>(H, 0));

	rep(h, H)
		rep(w, W)
			cin >> A[w][h];

	ll count = 0;

	rep(h, H)
	{
		rep(w, W)
		{
			if (A[w][h] != 1) continue;
			count++;

			queue<pair<int, int>> q;
			q.emplace(w, h);

			//幅優先探索
			while (!q.empty())
			{
				auto p = q.front(); q.pop();
				auto x = p.first;
				auto y = p.second;

				if (A[x][y] != 1) continue;
				A[x][y] = -1;

				if (IsInRange(x - 1, y))q.emplace(x - 1, y);
				if (IsInRange(x + 1, y))q.emplace(x + 1, y);
				if (IsInRange(x, y - 1))q.emplace(x, y - 1);
				if (IsInRange(x, y + 1))q.emplace(x, y + 1);
			}
		}
	}
		
	cout << count << endl;

	return 0;
}
0