結果

問題 No.697 池の数はいくつか
ユーザー vwxyz
提出日時 2023-11-28 15:37:22
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,780 ms / 6,000 ms
コード長 1,289 bytes
コンパイル時間 1,277 ms
コンパイル使用メモリ 99,364 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-09-26 12:59:27
合計ジャッジ時間 14,360 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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