結果

問題 No.697 池の数はいくつか
ユーザー vwxyzvwxyz
提出日時 2023-11-28 15:37:22
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,897 ms / 6,000 ms
コード長 1,289 bytes
コンパイル時間 1,404 ms
コンパイル使用メモリ 99,360 KB
実行使用メモリ 38,912 KB
最終ジャッジ日時 2023-11-28 15:37:37
合計ジャッジ時間 15,586 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 1 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 190 ms
7,552 KB
testcase_25 AC 188 ms
7,552 KB
testcase_26 AC 189 ms
7,552 KB
testcase_27 AC 189 ms
7,552 KB
testcase_28 AC 215 ms
7,552 KB
testcase_29 AC 1,892 ms
38,912 KB
testcase_30 AC 1,758 ms
38,656 KB
testcase_31 AC 1,897 ms
38,912 KB
testcase_32 AC 1,753 ms
38,656 KB
testcase_33 AC 1,727 ms
38,656 KB
testcase_34 AC 1,731 ms
38,656 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
    int H, W;
    cin >> H >> W;

    vector<vector<int>> A(H, vector<int>(W));
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            cin >> A[h][w];
        }
    }

    int ans = 0;
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            if (A[h][w]) {
                queue<pair<int, int>> q;
                q.push({h, w});
                A[h][w] = 0;

                while (!q.empty()) {
                    int hh = q.front().first;
                    int ww = q.front().second;
                    q.pop();

                    vector<pair<int, int>> directions = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
                    for (const auto& dir : directions) {
                        int dh = dir.first;
                        int dw = dir.second;
                        if (0 <= hh + dh && hh + dh < H && 0 <= ww + dw && ww + dw < W && A[hh + dh][ww + dw]) {
                            A[hh + dh][ww + dw] = 0;
                            q.push({hh + dh, ww + dw});
                        }
                    }
                }
                ans++;
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0