結果
| 問題 | 
                            No.697 池の数はいくつか
                             | 
                    
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-03-04 01:09:31 | 
| 言語 | C++17  (gcc 13.3.0 + boost 1.87.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 948 ms / 6,000 ms | 
| コード長 | 970 bytes | 
| コンパイル時間 | 1,955 ms | 
| コンパイル使用メモリ | 204,200 KB | 
| 最終ジャッジ日時 | 2025-01-19 09:50:30 | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 32 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
int dx[]={0,-1,0,1};
int dy[]={1,0,-1,0};
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int h,w; cin >> h >> w;
	vector<vector<bool>> f(h,vector<bool>(w,false));
	auto used=f;
	queue<int> q;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			int c; cin >> c;
			f[i][j]=c;
		}
	}
	int res=0;
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(f[i][j]){
				if(used[i][j])continue;
				res++;
				q.push(i*w+j);
				while(q.size()){
					int p=q.front(); q.pop();
					int x=p/w,y=p%w;
					for(int k=0;k<4;k++){
						int nx=x+dx[k],ny=y+dy[k];
						if(0<=nx and nx<h and 0<=ny and ny<w and f[nx][ny] and !used[nx][ny]){
							used[nx][ny]=true;
							q.push(nx*w+ny);
						}
					}
				}
			}
		}
	}
	printf("%d\n",res);
}