結果

問題 No.697 池の数はいくつか
ユーザー @abcde@abcde
提出日時 2019-03-29 00:25:56
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,460 ms / 6,000 ms
コード長 2,234 bytes
コンパイル時間 1,365 ms
コンパイル使用メモリ 169,612 KB
実行使用メモリ 101,584 KB
最終ジャッジ日時 2024-04-25 20:23:58
合計ジャッジ時間 17,386 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 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 1 ms
6,944 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 244 ms
16,460 KB
testcase_25 AC 240 ms
16,456 KB
testcase_26 AC 236 ms
14,544 KB
testcase_27 AC 247 ms
16,464 KB
testcase_28 AC 252 ms
16,464 KB
testcase_29 AC 1,506 ms
73,676 KB
testcase_30 AC 2,432 ms
101,452 KB
testcase_31 AC 1,552 ms
73,804 KB
testcase_32 AC 2,450 ms
101,580 KB
testcase_33 AC 2,453 ms
101,584 KB
testcase_34 AC 2,460 ms
101,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// bfsの動作確認用.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
constexpr int MAX = 9e6;
// constexpr int MAX = 64;
constexpr int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int H, W;
int board[MAX];
int memo[MAX];

// 幅優先探索.
// https://ja.wikipedia.org/wiki/幅優先探索
// 迷路を幅優先探索する.
// @param c: 探索地点の迷路の座標.
// @param l: 池の番号(※1以上).
// @param ret: n以上の素数.
void bfs(int c, int l){
    
    // 1. 終了条件設定.
    if(memo[c] >= 1)    return;
    if(board[c] == 0)   return;
    
    // 2. 空のキュー.
    queue<int> q;
    
    // 3. 訪問済みフラグ設定.
    memo[c] = l;
    
    // 4. 探索地点 c をキュー q に追加.
    q.push(c);
    while(!q.empty()){
        // 5. キューから取り出す.
        int v = q.front();
        q.pop();
        // 6. 取り出した要素を処理.
        // x: 列方向, y: 行方向 で考える.
        int nx, ny, n;
        int cx = v % W , cy = v / W;
        FOR(i, 0, 4){
            nx = cx + dx[i];
            ny = cy + dy[i];
            n = nx + ny * W;
            // cout << "cx=" << cx << " nx=" << nx  << " cy=" << cy << " ny=" << ny << " n=" << n << " c=" << c << endl;
            // 7. 訪問不可能なマス であれば, 処理をスキップ.
            if(n < 0 || nx < 0 || nx >= W || ny < 0 || ny >= H) continue;
            if(memo[n] == l)                  continue;
            // 8. 水のマスで, 訪問可能 かつ 未訪問 であれば, 訪問済みを設定.
            if(board[n] == 1 && memo[n] == 0) memo[n] = l, q.push(n);
        }
    }
    return;
}

int main() {
    
    // 1. 入力情報取得.
    cin >> H >> W;
    FOR(i, 0, H * W) cin >> board[i];
    
    // 2. 探索開始の頂点(根)を指定(頂点0番)し, 各頂点までの最短距離を保存.
    int counter = 1;
    FOR(i, 0, H * W) if(board[i] == 1) bfs(i, counter), counter++;
    
    // 3. 出力.
    // FOR(i, 0, H * W) cout << memo[i] << " ";
    // cout << endl;
    map<int, int> ans;
    FOR(i, 0, H * W) if(memo[i] > 0) ans[memo[i]]++;
    cout << ans.size() << endl;
    return 0;
    
}
0