結果

問題 No.697 池の数はいくつか
ユーザー CELICA
提出日時 2020-09-27 09:17:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 807 ms / 6,000 ms
コード長 976 bytes
コンパイル時間 1,944 ms
コンパイル使用メモリ 177,020 KB
実行使用メモリ 38,656 KB
最終ジャッジ日時 2024-11-08 08:45:23
合計ジャッジ時間 9,459 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ul = unsigned long;
using ull = unsigned long long;

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	int h, w;
	cin >> h >> w;

	vector<vector<int> > a(h, vector<int>(w));
	for (auto&& it : a)
	{
		for (auto&& it2 : it)
			cin >> it2;
	}

	int dx[4] = { 1,0,-1,0 },
		dy[4] = { 0,1,0,-1 };

	queue<pair<int, int> > q;
	int res{ 0 };

	for (int i = 0; i < h; ++i)
	{
		for (int j = 0; j < w; ++j)
		{
			if (a[i][j])
			{
				q.push(make_pair(i, j));
				a[i][j] = 0;
				++res;
			}

			while (!q.empty())
			{
				int x, y;
				tie(y, x) = q.front();
				for (int k = 0; k < 4; ++k)
				{
					int xx = x + dx[k];
					int yy = y + dy[k];

					if (xx < 0 || xx >= w)
						continue;
					if (yy < 0 || yy >= h)
						continue;

					if (a[yy][xx])
					{
						q.push(make_pair(yy, xx));
						a[yy][xx] = 0;
					}
				}
				q.pop();
			}
		}
	}
	cout << res << "\n";

	return 0;
}
0