結果

問題 No.697 池の数はいくつか
ユーザー face4face4
提出日時 2018-06-09 17:55:53
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 920 ms / 6,000 ms
コード長 939 bytes
コンパイル時間 895 ms
コンパイル使用メモリ 72,064 KB
実行使用メモリ 85,632 KB
最終ジャッジ日時 2024-11-08 07:34:27
合計ジャッジ時間 8,727 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<stack>
using namespace std;

#define W 3000
#define H 3000

char mat[H+1][W+1];
int cnt = 0;
int h, w;

void floodFill(int i, int j){
    stack<pair<int,int>> S;
    S.push({i,j});

    while(!S.empty()){
        pair<int,int> p = S.top(); S.pop();
        int y = p.first;
        int x = p.second;
        mat[y][x] = '0';

        if(y != 0 && mat[y-1][x] == '1')    S.push({y-1,x});
        if(y != h-1 && mat[y+1][x] == '1')  S.push({y+1,x});
        if(x != 0 && mat[y][x-1] == '1')    S.push({y,x-1});
        if(x != w-1 && mat[y][x+1] == '1')  S.push({y,x+1});
    }

    cnt++;
}

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

    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){ 
            if(mat[i][j]=='1')  floodFill(i,j);
        }
    }

    cout << cnt << endl;
    return 0;
}
0