結果

問題 No.697 池の数はいくつか
ユーザー @abcde@abcde
提出日時 2019-03-29 00:31:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,621 ms / 6,000 ms
コード長 3,015 bytes
コンパイル時間 1,416 ms
コンパイル使用メモリ 154,208 KB
実行使用メモリ 101,588 KB
最終ジャッジ日時 2023-08-08 02:25:14
合計ジャッジ時間 18,504 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,300 KB
testcase_01 AC 2 ms
5,524 KB
testcase_02 AC 2 ms
5,300 KB
testcase_03 AC 3 ms
5,452 KB
testcase_04 AC 2 ms
5,608 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,380 KB
testcase_07 AC 2 ms
5,300 KB
testcase_08 AC 2 ms
5,320 KB
testcase_09 AC 2 ms
5,352 KB
testcase_10 AC 2 ms
5,348 KB
testcase_11 AC 2 ms
5,284 KB
testcase_12 AC 2 ms
5,320 KB
testcase_13 AC 2 ms
5,304 KB
testcase_14 AC 2 ms
5,656 KB
testcase_15 AC 2 ms
5,500 KB
testcase_16 AC 2 ms
5,452 KB
testcase_17 AC 2 ms
5,300 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,540 KB
testcase_20 AC 2 ms
5,372 KB
testcase_21 AC 2 ms
5,300 KB
testcase_22 AC 2 ms
5,332 KB
testcase_23 AC 2 ms
5,404 KB
testcase_24 AC 257 ms
16,416 KB
testcase_25 AC 257 ms
16,480 KB
testcase_26 AC 257 ms
16,456 KB
testcase_27 AC 258 ms
16,632 KB
testcase_28 AC 257 ms
16,700 KB
testcase_29 AC 1,542 ms
73,596 KB
testcase_30 AC 2,621 ms
101,368 KB
testcase_31 AC 1,538 ms
73,732 KB
testcase_32 AC 2,614 ms
101,420 KB
testcase_33 AC 2,611 ms
101,528 KB
testcase_34 AC 2,617 ms
101,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// コメント修正して, 再提出.
#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/幅優先探索
// 迷路を幅優先探索する.
// ※bfsの動作確認用.
// @param c: 探索地点の迷路の座標.
// @param l: 池の番号(※1以上).
// @param: 特に無し.
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. 出力.
    // ex.
    // 10 12
    // 1 0 1 0 0 0 1 0 1 0 1 1
    // 1 1 1 0 0 0 1 0 0 1 0 1
    // 1 0 1 0 1 0 1 1 1 0 1 0
    // 1 0 1 0 0 0 1 1 1 0 0 1
    // 1 0 1 0 0 0 1 0 1 1 1 0
    // 1 0 1 0 0 1 1 1 1 1 1 0
    // 1 0 1 0 0 0 1 1 1 0 0 0
    // 1 0 1 0 0 1 1 0 1 1 1 0
    // 1 0 1 0 0 0 1 1 1 0 1 0
    // 1 0 1 0 0 0 1 1 0 1 0 1
    // 
    // 1 0 1 0 0   0 3 0 4  0  5  5 
    // 1 1 1 0 0   0 3 0 0 11  0  5 
    // 1 0 1 0 15  0 3 3 3  0 19  0 
    // 1 0 1 0 0   0 3 3 3  0  0 25 
    // 1 0 1 0 0   0 3 0 3  3  3  0 
    // 1 0 1 0 0   3 3 3 3  3  3  0 
    // 1 0 1 0 0   0 3 3 3  0  0  0 
    // 1 0 1 0 0   3 3 0 3  3  3  0 
    // 1 0 1 0 0   0 3 3 3  0  3  0 
    // 1 0 1 0 0   0 3 3 0 62  0 63 
    // -> 10個 で良さそう.

    // 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