結果

問題 No.697 池の数はいくつか
ユーザー YutaroYamanaka
提出日時 2019-06-14 23:43:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 1,746 ms / 6,000 ms
コード長 1,206 bytes
コンパイル時間 803 ms
コンパイル使用メモリ 64,752 KB
実行使用メモリ 74,112 KB
最終ジャッジ日時 2024-11-08 08:18:29
合計ジャッジ時間 14,177 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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