結果

問題 No.697 池の数はいくつか
ユーザー finefine
提出日時 2018-09-28 11:13:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 737 ms / 6,000 ms
コード長 1,066 bytes
コンパイル時間 1,697 ms
コンパイル使用メモリ 169,724 KB
実行使用メモリ 48,744 KB
最終ジャッジ日時 2024-04-25 20:20:11
合計ジャッジ時間 8,543 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 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,940 KB
testcase_14 AC 2 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 81 ms
7,536 KB
testcase_25 AC 85 ms
6,944 KB
testcase_26 AC 85 ms
7,744 KB
testcase_27 AC 84 ms
7,900 KB
testcase_28 AC 86 ms
7,364 KB
testcase_29 AC 672 ms
48,744 KB
testcase_30 AC 733 ms
12,328 KB
testcase_31 AC 689 ms
48,744 KB
testcase_32 AC 715 ms
12,352 KB
testcase_33 AC 711 ms
12,340 KB
testcase_34 AC 737 ms
12,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<int, int>;

const int dh[] = {1, -1, 0, 0};
const int dw[] = {0, 0, 1, -1};

int H, W;

bool f[3000][3000];

void dfs(int ch, int cw) {
    stack<P> s;
    s.emplace(ch, cw);
    f[ch][cw] = false;
    while (!s.empty()) {
        P p = s.top();
        s.pop();
        for (int i = 0; i < 4; i++) {
            int nh = p.first + dh[i], nw = p.second + dw[i];
            if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
            if (!f[nh][nw]) continue;
            f[nh][nw] = false;
            s.emplace(nh, nw);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            cin >> f[i][j];
        }
    }

    int ans = 0;
    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (f[i][j]) {
                dfs(i, j);
                ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0