結果

問題 No.697 池の数はいくつか
ユーザー sima.tettekesima.tetteke
提出日時 2019-12-12 11:57:22
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,919 bytes
コンパイル時間 1,508 ms
コンパイル使用メモリ 156,564 KB
実行使用メモリ 23,352 KB
最終ジャッジ日時 2023-08-16 23:23:20
合計ジャッジ時間 10,905 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,384 KB
testcase_17 AC 1 ms
4,384 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,384 KB
testcase_24 TLE -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
 
using namespace std;
typedef long long int ll;
typedef pair<ll, ll > pi;  
typedef pair<pair<ll, ll >, ll > pii;  
vector<ll > vec;
vector<vector<ll > > vec2;
ll MOD = 1000000007;
ll INF = 1145141919;
ll ans = 0;
ll N, M;
vector<vector<ll > > G;
 
ll dx[4] = {1, -1, 0, 0};
ll dy[4] = {0,  0, 1,-1};

//BFSで池のところがあれば塗りつぶすを繰り返す
void bfs(ll s_y, ll s_x){
    //準備
    vector<vector<ll > > dist(N, vector<ll >(M, -1));
    queue<pi> que;
    //初期化
    dist[s_y][s_x] = 0;
    que.push(make_pair(s_y, s_x));
    //一度通ったところは検索対象にしない
    G[s_y][s_x] = -1;
    while(!que.empty()){
        ll from_y = que.front().first;
        ll from_x = que.front().second;
        //二度と検索しないようにする
        
        que.pop();

        for(ll i = 0; i < 4; i++){
            if(from_y + dy[i] >= 0 && from_y + dy[i] < N && from_x + dx[i] >= 0 && from_x + dx[i] < M){
                ll to_y = from_y + dy[i];
                ll to_x = from_x + dx[i];
                //池ではない時
                if(G[to_y][to_x] == -1 || dist[to_y][to_x] != -1) continue;
 
                dist[to_y][to_x] = dist[from_y][from_x] + 1;
                que.push(make_pair(to_y, to_x));
                G[to_y][to_x] = -1;
            }
        }
    }
    
    ans++;
}
 
 
int main(){
 
    cin >> N >> M;
    G.assign(N, vector<ll >());
    
    //初期化
    for(ll i = 0; i < N; i++){
        for(ll j = 0; j < M; j++){
            ll t; cin >> t;
            if(t == 0){
                G[i].push_back(-1);
            }else{
                G[i].push_back(INF);
            }
            
        }       
    }
    
    for(ll i = 0; i < N; i++){
        for(ll j = 0; j < M; j++){
            if(G[i][j] == -1) continue;

            bfs(i, j);
        }       
    }
     
    cout << ans << endl;
 
}
0