結果

問題 No.697 池の数はいくつか
ユーザー mocobt
提出日時 2018-06-11 02:04:18
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,578 ms / 6,000 ms
コード長 1,192 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 74,880 KB
実行使用メモリ 38,528 KB
最終ジャッジ日時 2024-11-08 07:45:04
合計ジャッジ時間 12,987 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;
int H,W;
int A[3001][3001];
int res = 0;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, 1, 0, -1};

void dfs(int x, int y){ 
    A[y][x] = 0;
    if(y+1<H  && A[y+1][x]) dfs(x,y+1);
    if(y-1>=0 && A[y-1][x]) dfs(x,y-1);
    if(x+1<W  && A[y][x+1]) dfs(x+1,y);
    if(x-1>=0 && A[y][x-1]) dfs(x-1,y);
}

int main(){    
    cin >> H >> W;
    for(int j=0; j<H; j++) for(int i=0; i<W;i++) cin >> A[j][i]; 

    for(int j=0;j<H;j++){
        for(int i=0;i<W;i++){
            if(A[j][i]==0) continue;  
            queue<pair<int, int>> que;
            que.push(make_pair(j, i));
            A[j][i]=0;
            res++;

            while(!que.empty()){
                int y= que.front().first;
                int x = que.front().second;
                que.pop();
                for(int k=0; k<4; k++){
                    int ny = y+dy[k];
                    int nx = x+dx[k];
                    if (!(0 <= ny && ny < H && 0 <= nx && nx < W)) continue;
				    if (A[ny][nx] == 1) {
					    A[ny][nx] = 0;
					    que.push(make_pair(ny, nx));
				    }
                }
            }
        }
    }
    cout << res << endl;
}
0