結果

問題 No.697 池の数はいくつか
ユーザー Manuel1024
提出日時 2021-07-10 02:26:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,807 ms / 6,000 ms
コード長 1,268 bytes
コンパイル時間 1,011 ms
コンパイル使用メモリ 79,720 KB
実行使用メモリ 73,984 KB
最終ジャッジ日時 2024-11-08 08:58:45
合計ジャッジ時間 14,751 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
using P = pair<int, int>;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};

int main(){
    int h, w;
    cin >> h >> w;
    vector<vector<int>> s(h, vector<int>(w));
    for(auto &p: s){
        for(auto &pp: p) cin >> pp;
    }

    vector<vector<int>> visited(h, vector<int>(w, -1));
    int ans = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(s[i][j] == 1 && visited[i][j] == -1){
                queue<P> Q;
                Q.push(make_pair(i, j));
                visited[i][j] = ans;
                while(Q.size()){
                    auto cur = Q.front(); Q.pop();
                    for(int k = 0; k < 4; k++){
                        int nx = cur.first+dx[k];
                        int ny = cur.second+dy[k];
                        if(0 <= nx && nx < h
                        && 0 <= ny && ny < w
                        && s[nx][ny] == 1 && visited[nx][ny] == -1){
                            visited[nx][ny] = ans;
                            Q.push(make_pair(nx, ny));
                        }
                    }
                }
                ans++;
            }
        }
    }

    cout << ans << endl;
    return 0;
}
0