結果

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

vector<vector<bool>> field;
vector<bool> x;


void dfs(int y, int x){
    field[y][x] = 0;
    if (field[y+1][x])
        dfs(y+1, x);
    if (field[y-1][x])
        dfs(y-1, x);
    if (field[y][x+1])
        dfs(y, x+1);
    if (field[y][x-1])
        dfs(y, x-1);
    return;
}

void bfs(int y, int x){
    vector<pair<int, int>> search;
    vector<pair<int, int>> new_search;
    field[y][x] = 0;
    search.push_back(make_pair(y, x));
    while (!search.empty()){
        for (auto z : search){
            if (field[z.first+1][z.second]){
                field[z.first+1][z.second] = 0;
                new_search.push_back(make_pair(z.first+1, z.second));
            }
            if (field[z.first-1][z.second]){
                field[z.first-1][z.second] = 0;
                new_search.push_back(make_pair(z.first-1, z.second));
            }
            if (field[z.first][z.second+1]){
                field[z.first][z.second+1] = 0;
                new_search.push_back(make_pair(z.first, z.second+1));
            }
            if (field[z.first][z.second-1]){
                field[z.first][z.second-1] = 0;
                new_search.push_back(make_pair(z.first, z.second-1));
            }
        }
        
        search.clear();
        search = new_search;
        new_search.clear();
    }
    return;
}


int main(){
    int h, w, k, counter;
    cin >> h >> w;
    counter = 0;
    
    for (int i = 0; i < w + 2; i++)
        x.push_back(0);
    field.push_back(x);
    x.clear();
    
    for (int i = 0; i < h; i++){
        x.push_back(0);
        for (int j = 0; j < w; j++){
            cin >> k;
            x.push_back(k);
        }
        x.push_back(0);
        field.push_back(x);
        x.clear();
    }

    for (int i = 0; i < w + 2; i++)
        x.push_back(0);
    field.push_back(x);
    
    // for (auto v : field){
    //     for (auto w : v)
    //         cout << w;
    //     cout << endl;
    // }
    
    for (int i = 0; i < h; i++){
        for (int j = 0; j < w; j++){
            if (field[i+1][j+1]){
                bfs(i+1, j+1);
                counter++;
            }
        }
    }
    
    // for (auto v : field){
    //     for (auto w : v)
    //         cout << w;
    //     cout << endl;
    // }
    
    cout << counter << endl;
    return 0;

}
0