結果

問題 No.697 池の数はいくつか
ユーザー Pachicobue
提出日時 2018-06-18 15:45:22
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,681 ms / 6,000 ms
コード長 995 bytes
コンパイル時間 4,303 ms
コンパイル使用メモリ 81,512 KB
最終ジャッジ日時 2025-01-05 14:40:19
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main()
{
    constexpr int d[]={-1,0,1,0,-1};
    int H, W;cin >> H >> W;
    vector<vector<int>> f(H, vector<int>(W));
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){cin >> f[i][j];}
    }
    using P = pair<int,int>;
    auto in = [&](const int y, const int x){return y >= 0 and y < H and x >= 0 and x < W and f[y][x]==1;};
    int ans = 0;
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            if(f[i][j]==0){continue;}
            queue<P> q;
            ans++,f[i][j]=0,q.push({i,j});
            while(not q.empty()){
                const int y = q.front().first,x=q.front().second;
                q.pop();
                for(int k = 0; k < 4; k++){
                    const int ny = y+d[k],nx = x+d[k+1];
                    if(in(ny,nx)){f[ny][nx]=0,q.push({ny,nx});}
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0