結果

問題 No.697 池の数はいくつか
ユーザー Konton7Konton7
提出日時 2020-01-14 16:22:59
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,461 ms / 6,000 ms
コード長 2,390 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 208,984 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-25 20:38:26
合計ジャッジ時間 13,450 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 1 ms
6,944 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 162 ms
6,944 KB
testcase_25 AC 164 ms
6,940 KB
testcase_26 AC 164 ms
6,944 KB
testcase_27 AC 158 ms
6,940 KB
testcase_28 AC 156 ms
6,940 KB
testcase_29 AC 1,335 ms
6,940 KB
testcase_30 AC 1,451 ms
6,944 KB
testcase_31 AC 1,290 ms
6,944 KB
testcase_32 AC 1,442 ms
6,944 KB
testcase_33 AC 1,461 ms
6,940 KB
testcase_34 AC 1,426 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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