結果

問題 No.697 池の数はいくつか
ユーザー H3PO4H3PO4
提出日時 2023-03-31 14:22:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,601 bytes
コンパイル時間 1,963 ms
コンパイル使用メモリ 88,600 KB
実行使用メモリ 814,932 KB
最終ジャッジ日時 2023-10-24 01:47:27
合計ジャッジ時間 8,788 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

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

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

    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();
                    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