結果

問題 No.697 池の数はいくつか
ユーザー lapilapi
提出日時 2019-07-03 14:10:03
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 698 ms / 6,000 ms
コード長 1,433 bytes
コンパイル時間 1,061 ms
コンパイル使用メモリ 106,736 KB
実行使用メモリ 12,432 KB
最終ジャッジ日時 2024-04-25 20:29:42
合計ジャッジ時間 7,191 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,948 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 77 ms
6,944 KB
testcase_25 AC 80 ms
6,980 KB
testcase_26 AC 76 ms
7,536 KB
testcase_27 AC 78 ms
7,628 KB
testcase_28 AC 79 ms
7,052 KB
testcase_29 AC 617 ms
12,420 KB
testcase_30 AC 677 ms
12,400 KB
testcase_31 AC 624 ms
12,432 KB
testcase_32 AC 685 ms
12,340 KB
testcase_33 AC 685 ms
12,360 KB
testcase_34 AC 698 ms
12,284 KB
権限があれば一括ダウンロードができます

ソースコード

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