結果

問題 No.697 池の数はいくつか
ユーザー H3PO4H3PO4
提出日時 2023-03-31 14:29:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,063 ms / 6,000 ms
コード長 1,699 bytes
コンパイル時間 1,066 ms
コンパイル使用メモリ 87,256 KB
実行使用メモリ 79,512 KB
最終ジャッジ日時 2023-10-24 01:52:45
合計ジャッジ時間 15,538 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,368 KB
testcase_01 AC 2 ms
4,368 KB
testcase_02 AC 2 ms
4,368 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 2 ms
4,368 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 2 ms
4,368 KB
testcase_08 AC 2 ms
4,368 KB
testcase_09 AC 2 ms
4,368 KB
testcase_10 AC 2 ms
4,368 KB
testcase_11 AC 2 ms
4,368 KB
testcase_12 AC 2 ms
4,368 KB
testcase_13 AC 2 ms
4,368 KB
testcase_14 AC 2 ms
4,368 KB
testcase_15 AC 2 ms
4,368 KB
testcase_16 AC 2 ms
4,368 KB
testcase_17 AC 2 ms
4,368 KB
testcase_18 AC 2 ms
4,368 KB
testcase_19 AC 2 ms
4,368 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 2 ms
4,368 KB
testcase_22 AC 2 ms
4,368 KB
testcase_23 AC 2 ms
4,368 KB
testcase_24 AC 195 ms
4,368 KB
testcase_25 AC 195 ms
4,368 KB
testcase_26 AC 196 ms
4,368 KB
testcase_27 AC 195 ms
4,368 KB
testcase_28 AC 196 ms
4,368 KB
testcase_29 AC 2,063 ms
79,512 KB
testcase_30 AC 1,749 ms
6,180 KB
testcase_31 AC 2,059 ms
79,512 KB
testcase_32 AC 1,745 ms
6,180 KB
testcase_33 AC 1,743 ms
6,180 KB
testcase_34 AC 1,742 ms
6,180 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<stack>

int main() {
    int H, W;
    std::cin >> H >> W;
    std::vector<std::vector<bool>> A(H, std::vector<bool>(W));
    for (int h = 0; h < H; ++h) {
        for (int w = 0; w < W; ++w) {
            int a;
            std::cin >> a;
            A.at(h).at(w) = bool(a);
        }
    }

    const std::vector<std::pair<int, int>> dhw = {{0,  1},
                                                  {1,  0},
                                                  {0,  -1},
                                                  {-1, 0}};

    std::vector<std::vector<bool>> nonvisited(H, std::vector<bool>(W, true));
    int ans = 0;

    for (int h0 = 0; h0 < H; ++h0) {
        for (int w0 = 0; w0 < W; ++w0) {
            if (A.at(h0).at(w0) && nonvisited.at(h0).at(w0)) {
                ans++;
                std::stack<std::pair<int, int>> stk;
                stk.emplace(h0, w0);
                while (!stk.empty()) {
                    const auto [h, w] = stk.top();
                    stk.pop();
                    nonvisited.at(h).at(w) = false;
                    for (const auto &[dh, dw]: dhw) {
                        const int hdh = h + dh;
                        const int wdw = w + dw;
                        if (0 <= hdh
                            && hdh < H
                            && 0 <= wdw
                            && wdw < W
                            && A.at(hdh).at(wdw)
                            && nonvisited.at(hdh).at(wdw)) {
                            stk.emplace(hdh, wdw);
                        }
                    }
                }
            }
        }
    }
    std::cout << ans << std::endl;
}
0