結果

問題 No.697 池の数はいくつか
ユーザー 萃罰
提出日時 2023-08-05 20:56:23
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,553 ms / 6,000 ms
コード長 1,117 bytes
コンパイル時間 3,868 ms
コンパイル使用メモリ 177,624 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-10-15 17:57:47
合計ジャッジ時間 16,505 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ba = pair<int,int>;
int main(){
    int h,w;
    cin >> h>>w;
    vector<vector<int>> gra(h);
    for(int i=0;i<h;i++){
        vector<int> g(w);
        for(int j=0;j<w;j++){
            cin >> g[j];
        }
        gra[i] = g;
    }
    queue<ba> que;
    int hou[4][2]= {{0,1},{1,0},{-1,0},{0,-1}};
    int ans = 0;
    for(int i = 0;i<h;i++){
        for(int j=0;j<w;j++){
            if (gra[i][j] == 0) continue;
            ba fi = {i,j};
            que.push(fi);
            gra[i][j] = 0;
            while(!que.empty()){
                ba pos = que.front();
                que.pop();
                for(int L =0;L<4;L++){
                    int x = hou[L][0]+pos.first;
                    int y = hou[L][1]+pos.second;
                    if(0<=x&&x<h&&0<=y&&y<w){
                        if(gra[x][y] == 0) continue;
                        gra[x][y] = 0;
                        ba pu = {x,y};
                        que.push(pu);
                    }
                }
            }
            ans++;
        }
    }
    cout << ans<<endl;
}
0