結果

問題 No.697 池の数はいくつか
ユーザー YutaroYamanakaYutaroYamanaka
提出日時 2019-06-14 23:43:05
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,624 ms / 6,000 ms
コード長 1,206 bytes
コンパイル時間 511 ms
コンパイル使用メモリ 66,232 KB
実行使用メモリ 74,204 KB
最終ジャッジ日時 2024-04-25 20:30:24
合計ジャッジ時間 12,696 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 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 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 1 ms
6,944 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 1 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 182 ms
28,284 KB
testcase_25 AC 176 ms
30,468 KB
testcase_26 AC 174 ms
28,216 KB
testcase_27 AC 175 ms
28,152 KB
testcase_28 AC 177 ms
28,420 KB
testcase_29 AC 1,530 ms
74,196 KB
testcase_30 AC 1,597 ms
74,068 KB
testcase_31 AC 1,509 ms
74,200 KB
testcase_32 AC 1,610 ms
74,204 KB
testcase_33 AC 1,624 ms
74,076 KB
testcase_34 AC 1,582 ms
74,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

queue< pair <int,int> > Q;
int H, W;
int A[3010][3010];
int arrive[3010][3010];
int dy[4] = { 1, 0, -1, 0 }, dx[4] = { 0, 1, 0, -1 };


int bfs(int y, int x){
    if(arrive[y][x] == 2) return 0;
    Q.push(make_pair(y, x));
    int v, w;
    while(!Q.empty()){
        v = Q.front().first;
        w = Q.front().second;
        arrive[v][w] = 2;
        Q.pop();
        
        for(int i = 0; i < 4; i++){
            int ny = v + dy[i];
            int nx = w + dx[i];
            if(ny < 0 || ny >= H || nx < 0 || nx >= W || arrive[ny][nx] != 0 || A[ny][nx]==0){
                continue;
            }
            Q.push(make_pair(ny, nx));
            arrive[ny][nx] = 1;
        }
    }
    
    return 1;
}

int solve() {
    int ans = 0;
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++){
            if(A[i][j] == 1) ans += bfs(i, j);
        }
    }
    
    return ans;
}

int main(void){
    // Your code here!
    cin >> H >> W;
    for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) cin >> A[i][j];
    for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) arrive[i][j] = 0;
    cout << solve() << endl;
    
}
0