ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 1697번 C++ 풀이
    카테고리 없음 2018. 11. 11. 02:34
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <math.h>
    #include <climits>

    using namespace std;
    bool isVisted[100001];
    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    int time = 0;
    queue<int> q;
    isVisted[n] = true;
    q.push(n);
    if (n == k){
    cout << 0;
    return 0;
    }
    while (!q.empty()){
    time++;
    int size = q.size();
    for (auto i=0; i<size; i++){
    int top = q.front();

    q.pop();
    int candidate[3] = {top-1, top+1, 2 * top};
    for (auto i =0; i<3; i++){
    if (candidate[i] >= 0 && candidate[i] <= 100000){
    if (!isVisted[candidate[i]]){
    isVisted[candidate[i]] = true;
    if ( candidate[i] == k){
    goto theEnd;
    }
    q.push(candidate[i]);
    }
    }
    }
    }
    }
    theEnd:
    cout << time;
    return 0;
    }


Designed by Tistory.