結果

問題 No.697 池の数はいくつか
ユーザー srjywrdnprkt
提出日時 2023-01-12 13:16:48
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,683 ms / 6,000 ms
コード長 1,484 bytes
コンパイル時間 1,092 ms
コンパイル使用メモリ 116,484 KB
最終ジャッジ日時 2025-02-10 01:50:01
ジャッジサーバーID
(参考情報)
judge5 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>
#include <algorithm>
#include <numeric>
#include <deque>
#include <complex>
#include <cassert>

using namespace std;

int main(){

    int H, W, h, w, nh, nw, ans=0;
    cin >> H >> W;
    
    queue<pair<int, int>> que;
    int dx[4] = {1, 0, -1, 0};
    int dy[4] = {0, 1, 0, -1};
    vector<vector<int>> dist(H, vector<int>(W, -1));
    vector<vector<int>> field(H, vector<int>(W));

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

    for (int i=0; i<H; i++){
        for (int j=0; j<W; j++){
            if (field[i][j] == 1 && dist[i][j] == -1){
                ans++;
                que.push({i, j});
                dist[i][j] = 0;
                while(!que.empty()){
                    tie(h, w) = que.front();
                    que.pop();

                    for (int dir=0; dir < 4; dir++){
                        nh = h + dx[dir];
                        nw = w + dy[dir];
                        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
                        if (field[nh][nw] == 0) continue;
                        if (dist[nh][nw] != -1) continue;
                        dist[nh][nw] = dist[h][w] + 1;
                        que.push({nh, nw});
                    }
                }
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0