結果

問題 No.697 池の数はいくつか
ユーザー wunderkammer2
提出日時 2020-02-26 21:31:39
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,947 ms / 6,000 ms
コード長 1,342 bytes
コンパイル時間 1,086 ms
コンパイル使用メモリ 104,288 KB
実行使用メモリ 130,420 KB
最終ジャッジ日時 2024-11-08 08:35:44
合計ジャッジ時間 15,379 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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>
#include <stack>

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()

short H, W;

bool IsInRange(short w, short 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++;

			stack<pair<short, short>> q;
			q.emplace(w, h);

			//深さ優先探索
			while (!q.empty())
			{
				auto p = q.top(); 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