結果

問題 No.697 池の数はいくつか
ユーザー KhiromuKhiromu
提出日時 2018-06-09 16:24:09
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2,044 ms / 6,000 ms
コード長 1,999 bytes
コンパイル時間 1,770 ms
コンパイル使用メモリ 165,500 KB
実行使用メモリ 54,180 KB
最終ジャッジ日時 2024-04-25 19:51:58
合計ジャッジ時間 16,259 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 1 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 1 ms
6,940 KB
testcase_15 AC 1 ms
6,944 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,940 KB
testcase_18 AC 1 ms
6,940 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,944 KB
testcase_24 AC 195 ms
18,868 KB
testcase_25 AC 195 ms
18,636 KB
testcase_26 AC 201 ms
18,952 KB
testcase_27 AC 206 ms
18,640 KB
testcase_28 AC 208 ms
18,824 KB
testcase_29 AC 2,044 ms
53,548 KB
testcase_30 AC 1,741 ms
53,848 KB
testcase_31 AC 2,007 ms
53,416 KB
testcase_32 AC 1,712 ms
54,180 KB
testcase_33 AC 1,708 ms
53,616 KB
testcase_34 AC 1,898 ms
53,504 KB
権限があれば一括ダウンロードができます

ソースコード

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