結果

問題 No.697 池の数はいくつか
ユーザー Mcpu3
提出日時 2019-08-15 00:10:12
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,710 ms / 6,000 ms
コード長 1,631 bytes
コンパイル時間 1,079 ms
コンパイル使用メモリ 85,428 KB
実行使用メモリ 40,064 KB
最終ジャッジ日時 2024-11-08 08:21:16
合計ジャッジ時間 13,936 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

int main() {
	int H, W, ans = 0;
	cin >> H >> W;
	vector<vector<int>> A(H, vector<int>(W));
	for (vector<int>& i : A) {
		for (int& j : i) cin >> j;
	}
	vector<vector<bool>> a(H, vector<bool>(W));
	for (int i = 0; i < H; ++i) {
		for (int j = 0; j < W; ++j) {
			if (!a[i][j] && A[i][j]) {
				++ans;
				a[i][j] = true;
				queue<pair<int, int>> b;
				b.emplace(i, j);
				while (!b.empty()) {
					if (H - 1 != b.front().first) {
						if (!a[1 + b.front().first][b.front().second] && A[1 + b.front().first][b.front().second]) {
							a[1 + b.front().first][b.front().second] = true;
							b.emplace(1 + b.front().first, b.front().second);
						}
					}
					if (W - 1 != b.front().second) {
						if (!a[b.front().first][1 + b.front().second] && A[b.front().first][1 + b.front().second]) {
							a[b.front().first][1 + b.front().second] = true;
							b.emplace(b.front().first, 1 + b.front().second);
						}
					}
					if (b.front().first) {
						if (!a[b.front().first - 1][b.front().second] && A[b.front().first - 1][b.front().second]) {
							a[b.front().first - 1][b.front().second] = true;
							b.emplace(b.front().first - 1, b.front().second);
						}
					}
					if (b.front().second) {
						if (!a[b.front().first][b.front().second - 1] && A[b.front().first][b.front().second - 1]) {
							a[b.front().first][b.front().second - 1] = true;
							b.emplace(b.front().first, b.front().second - 1);
						}
					}
					b.pop();
				}
			}
		}
	}
	cout << ans;
}
0