結果

問題 No.697 池の数はいくつか
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-01-12 13:16:48
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,661 ms / 6,000 ms
コード長 1,484 bytes
コンパイル時間 1,127 ms
コンパイル使用メモリ 118,092 KB
実行使用メモリ 73,896 KB
最終ジャッジ日時 2023-08-24 20:48:41
合計ジャッジ時間 14,274 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,384 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 184 ms
11,252 KB
testcase_25 AC 184 ms
11,504 KB
testcase_26 AC 185 ms
11,504 KB
testcase_27 AC 183 ms
11,248 KB
testcase_28 AC 184 ms
11,268 KB
testcase_29 AC 1,531 ms
73,896 KB
testcase_30 AC 1,657 ms
73,436 KB
testcase_31 AC 1,538 ms
73,888 KB
testcase_32 AC 1,660 ms
73,608 KB
testcase_33 AC 1,661 ms
73,436 KB
testcase_34 AC 1,659 ms
73,548 KB
権限があれば一括ダウンロードができます

ソースコード

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