結果

問題 No.697 池の数はいくつか
ユーザー Khiromu
提出日時 2018-06-09 16:24:09
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2,241 ms / 6,000 ms
コード長 1,999 bytes
コンパイル時間 1,849 ms
コンパイル使用メモリ 164,240 KB
実行使用メモリ 45,056 KB
最終ジャッジ日時 2024-11-08 07:33:14
合計ジャッジ時間 17,060 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define CK(N, A, B) (A <= N && N < B)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define RREP(i, a, b) for (int i = (b - 1); a <= i; i--)
#define p(s) cout<<(s)<<endl
#define F first
#define S second
typedef long long ll;

const int INF = 1e9;
const long long LLINF = 1e18;

using namespace std;

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

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

bool visited[10010][10010];
bool field[10010][10010];    //格子状のフィールド (配列)
void bfs_field() {
    queue<pair<int,pair<int,int> > > q; //<コスト, 座標(y,x)>
    q.push({0, {start_y, start_x}});  //開始ノードを追加

    if(!visited[start_y][start_x]) ans++;

    while(!q.empty()){
        int cur_y = q.front().second.first;
        int cur_x = q.front().second.second;
        int curStep = q.front().first;
        q.pop();

        if(visited[cur_y][cur_x]) continue;
        visited[cur_y][cur_x] = true;

        REP(k,0,4){
            //REP(k,0,8){
            int next_y = cur_y + dy[k];
            int next_x = cur_x + dx[k];
            //int next_y = cur_y + dy8[k];
            //int next_x = cur_y + dx8[k];

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

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

    REP(i, 0, H){
        REP(j, 0, W){
            start_y=i;
            start_x=j;
            if(field[start_y][start_x]==0) continue;
            bfs_field();
        }
    }

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