結果

問題 No.697 池の数はいくつか
ユーザー dnish
提出日時 2018-06-23 01:19:47
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,622 ms / 6,000 ms
コード長 1,737 bytes
コンパイル時間 3,110 ms
コンパイル使用メモリ 171,484 KB
実行使用メモリ 38,784 KB
最終ジャッジ日時 2024-11-08 07:55:52
合計ジャッジ時間 14,422 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
#define REP(i,n,N) for(int i=(n); i<(N); i++)
#define RREP(i,n,N) for(ll i=(N-1); i>=n; i--)
#define CK(n,a,b) ((a)<=(n)&&(n)<(b))
#define ALL(v) (v).begin(),(v).end()
#define p(s) cout<<(s)<<endl
#define p2(a,b) cout<<(a)<<" "<<(b)<<endl
#define v2(T) vector<vector<T>>
typedef long long ll;
using namespace std;
const ll mod= 1e10;

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

int H;  //フィールドの高さ
int W;  //フィールドの幅
int start_y, start_x;    //開始座標

const int NODE_SIZE = 3010;

int field[NODE_SIZE][NODE_SIZE];      //格子状のフィールド (string)

//幅優先探索 (フィールド: 配列/string)
void bfs_field() {
    queue<pair<int,int>> q; //<座標(y,x)>
    q.push({start_y, start_x});  //開始ノードを追加

    while(!q.empty()){
        int cur_y = q.front().first;
        int cur_x = q.front().second;
        q.pop();
        if(field[cur_y][cur_x]==0) continue;
        field[cur_y][cur_x]=0;
        REP(k,0,4){
            int next_y = cur_y + dy[k];
            int next_x = cur_x + dx[k];

            if(!CK(next_y,0,H) || !CK(next_x,0,W)) continue;  //範囲外
            /* ここに問題ごとの条件 */
            if(field[next_y][next_x]){     //未到達の座標だけpush.
                q.push({next_y, next_x});
            }
        }
    }
}

int main(){
    cin>>H>>W;
    REP(i,0,H) {
        REP(j, 0, W) {
            cin >> field[i][j];

        }
    }
    int ans=0;
    REP(i,0,H){
        REP(j,0,W){
            if(field[i][j]){
                start_y = i;
                start_x = j;
                bfs_field();
                ans++;
            }
        }
    }
    p(ans);
    return 0;
}
0