結果

問題 No.697 池の数はいくつか
ユーザー tokizotokizo
提出日時 2019-02-01 16:43:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 864 ms / 6,000 ms
コード長 1,238 bytes
コンパイル時間 1,771 ms
コンパイル使用メモリ 169,496 KB
実行使用メモリ 47,716 KB
最終ジャッジ日時 2024-04-25 20:23:22
合計ジャッジ時間 9,153 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 3 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 2 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 2 ms
6,944 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 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,944 KB
testcase_22 AC 2 ms
6,940 KB
testcase_23 AC 2 ms
6,944 KB
testcase_24 AC 100 ms
18,656 KB
testcase_25 AC 102 ms
18,532 KB
testcase_26 AC 101 ms
18,660 KB
testcase_27 AC 100 ms
20,708 KB
testcase_28 AC 101 ms
20,836 KB
testcase_29 AC 772 ms
47,716 KB
testcase_30 AC 856 ms
47,588 KB
testcase_31 AC 767 ms
47,464 KB
testcase_32 AC 864 ms
47,584 KB
testcase_33 AC 857 ms
47,588 KB
testcase_34 AC 853 ms
47,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int h, w;
const int MAX = 3010;
int A[MAX][MAX];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool seen[MAX][MAX];

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 >> A[i][j];
        }
    }

    int ans = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(A[i][j] == 1 and !seen[i][j]){
                ans++;

                queue<pair<int, int>> q;
                seen[i][j] = true;
                q.push({i, j});
                while(!q.empty()){
                    pair<int, int> p = q.front();
                    q.pop();

                    for(int k = 0; k < 4; k++){
                        int nx = dx[k] + p.first;
                        int ny = dy[k] + p.second;
                        if(nx < 0 or nx >= h or ny < 0 or ny >= w) continue;
                        if(seen[nx][ny] or A[nx][ny] == 0) continue;
                        seen[nx][ny] = true;
                        q.push({nx, ny});
                    }
                }
            }
        }
    }
    cout << ans << endl;

    return 0;
}
0