結果

問題 No.697 池の数はいくつか
ユーザー とばりとばり
提出日時 2018-09-06 11:29:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 899 ms / 6,000 ms
コード長 1,420 bytes
コンパイル時間 1,886 ms
コンパイル使用メモリ 168,956 KB
実行使用メモリ 47,588 KB
最終ジャッジ日時 2024-04-25 20:17:55
合計ジャッジ時間 9,565 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,940 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,944 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 3 ms
6,940 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,944 KB
testcase_18 AC 2 ms
6,940 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 104 ms
18,792 KB
testcase_25 AC 105 ms
20,836 KB
testcase_26 AC 104 ms
18,660 KB
testcase_27 AC 106 ms
20,832 KB
testcase_28 AC 105 ms
20,708 KB
testcase_29 AC 783 ms
47,456 KB
testcase_30 AC 889 ms
47,460 KB
testcase_31 AC 781 ms
47,332 KB
testcase_32 AC 893 ms
47,456 KB
testcase_33 AC 893 ms
47,584 KB
testcase_34 AC 899 ms
47,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using int64 = long long;
using uint64 = unsigned long long;

int dr[4] = {1, 0, -1, 0},
    dc[4] = {0, 1, 0, -1};

const int MAX_H = 3000;
const int MAX_W = 3000;

int H, W, A[MAX_H][MAX_W];
bool isVisited[MAX_H][MAX_W];

bool isInside(int r, int c)
{
    return 0 <= r and r < H and 0 <= c and c < W;
}

void bfs(int r, int c)
{
    queue<pair<int, int>> Q;
    isVisited[r][c] = true;
    Q.push(make_pair(r, c));

    while (!Q.empty())
    {
        auto pos = Q.front(); Q.pop();

        for (int i = 0; i < 4; i++)
        {
            int nr = pos.first + dr[i],
                nc = pos.second + dc[i];
            if (isInside(nr, nc) and !isVisited[nr][nc] and A[nr][nc] == 1)
            {
                isVisited[nr][nc] = true;
                Q.push(make_pair(nr, nc));
            }
        }
    }
}

int counting()
{
    int cnt = 0;

    for (int r = 0; r < H; r++)
    {
        for (int c = 0; c < W; c++)
        {
            if (!isVisited[r][c] and A[r][c] == 1)
            {
                cnt++;
                bfs(r, c);
            }
        }
    }

    return cnt;
}

int main()
{
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    cin >> H >> W;

    for (int r = 0; r < H; r++)
    {
        for (int c = 0; c < W; c++)
        {
            cin >> A[r][c];
        }
    }

    cout << counting() << endl;

    return 0;
}
0