結果

問題 No.697 池の数はいくつか
ユーザー CELICACELICA
提出日時 2020-09-27 09:17:32
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 784 ms / 6,000 ms
コード長 976 bytes
コンパイル時間 2,044 ms
コンパイル使用メモリ 173,232 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-04-25 20:53:01
合計ジャッジ時間 8,855 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,812 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 86 ms
7,296 KB
testcase_25 AC 85 ms
7,296 KB
testcase_26 AC 86 ms
7,168 KB
testcase_27 AC 87 ms
7,168 KB
testcase_28 AC 87 ms
7,168 KB
testcase_29 AC 771 ms
38,784 KB
testcase_30 AC 776 ms
38,528 KB
testcase_31 AC 775 ms
38,656 KB
testcase_32 AC 772 ms
38,528 KB
testcase_33 AC 784 ms
38,528 KB
testcase_34 AC 779 ms
38,656 KB
権限があれば一括ダウンロードができます

ソースコード

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