結果

問題 No.697 池の数はいくつか
ユーザー tokizo
提出日時 2019-02-01 16:08:20
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
MLE  
実行時間 -
コード長 965 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 167,352 KB
実行使用メモリ 610,020 KB
最終ジャッジ日時 2024-11-25 14:41:40
合計ジャッジ時間 9,678 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int h, w;
const int MAX = 3010;
int A[MAX][MAX];
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
bool seen[MAX][MAX];

void dfs(int x, int y){
    seen[x][y] = true;
    for(int i = 0; i < 4; i++){
        int nx, ny;
        nx = x + dx[i];
        ny = y + dy[i];
        if(nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
        if(A[nx][ny] == 0 or seen[nx][ny]) continue;
        dfs(nx, ny);
    }
    return;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> h >> w;

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> A[i][j];
        }
    }

    int ans = 0;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(A[i][j] == 1){
                if(!seen[i][j]){
                    ans++;
                    dfs(i, j);
                }
            }
        }
    }
    cout << ans << endl;

    return 0;
}
0