結果

問題 No.697 池の数はいくつか
ユーザー lapi
提出日時 2019-07-03 14:10:03
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 739 ms / 6,000 ms
コード長 1,433 bytes
コンパイル時間 990 ms
コンパイル使用メモリ 107,016 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-11-08 08:17:43
合計ジャッジ時間 7,722 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <set>
#include <map>
#include <numeric>
#include <regex>
#include <tuple>
#include<iomanip>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
#define MOD 1000000007 // 10^9 + 7
#define INF 1000000000 // 10^9
#define LLINF 1LL<<60

int H, W;
bool field[3009][3009];

// 移動4方向のベクトル
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };

// x,yが範囲に入っているかどうか
// 1 <= x <= N かつ 1 <= y <= Mが満たされてるかどうか
bool isrange(int x, int y) {
	bool flag = true;
	if (x < 1 || H < x) flag = false;
	if (y < 1 || W < y) flag = false;
	return flag;
}

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

	cin >> H >> W;
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) cin >> field[i][j];
	}

	queue<P> Q;
	int ans = 0;

	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			if (field[i][j]) {
				ans++;
				field[i][j] = false;
				Q.push(P(i, j));

				while (!Q.empty()) {
					int cx = Q.front().first;
					int cy = Q.front().second;
					Q.pop();

					for (int k = 0; k < 4; k++) {
						int nx = cx + dx[k];
						int ny = cy + dy[k];

						if (field[nx][ny]) {
							field[nx][ny] = false;
							Q.push(P(nx, ny));
						}
					}
				}
			}
		}
	}

	cout << ans << endl;

	return 0;
}
0