結果

問題 No.697 池の数はいくつか
ユーザー srjywrdnprkt
提出日時 2023-01-12 13:10:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 1,040 bytes
コンパイル時間 1,318 ms
コンパイル使用メモリ 115,400 KB
最終ジャッジ日時 2025-02-10 01:49:44
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <queue>

using namespace std;

int H, W;

vector<vector<bool>> visit;
vector<vector<int>> field;

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int h, int w){
    int nh, nw;

    visit[h][w] = 1;

    for (int dir=0; dir < 4; dir++){
        nh = h + dx[dir];
        nw = w + dy[dir];

        if (nh < 0 || nh >= H || nw < 0 || nw >= W) continue;
        if (field[nh][nw] == 0) continue;
        if (visit[nh][nw]) continue;

        dfs(nh, nw);
    }
}

int main(){

    int ans=0;
    cin >> H >> W;
    field.resize(H, vector<int>(W));
    visit.resize(H, vector<bool>(W));

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

    for (int i=0; i<H; i++){
        for (int j=0; j<W; j++){
            if (!visit[i][j] && field[i][j] == 1){
                ans++;
                dfs(i, j);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0