3.1. 15이원준 ¶
#include<iostream> #include<cmath> #include<algorithm> #include<queue> #include<utility> using namespace std; int main(){ int arr[100001] = {0,}; int N, M, ans; queue<pair<int, int>> que; cin>> N >> M; if(N >= M){ cout<< N - M << endl; return 0; } que.push(make_pair(N, 0)); while(que.front().first != M){ int n, dep; n = que.front().first; dep = que.front().second; que.pop(); if(n-1 >= 0 && !arr[n-1]){ arr[n-1] = 1; que.push(make_pair(n - 1, dep + 1)); } if(n+1 <= 100000 && !arr[n+1]){ arr[n+1] = 1; que.push(make_pair(n + 1, dep + 1)); } if(n * 2 <= 100000 && !arr[n*2]){ arr[n*2] = 1; que.push(make_pair(n * 2, dep + 1)); } } cout<< que.front().second << endl; }
3.2. 박인서 ¶
#include <iostream> #include <queue> typedef std::pair<int, int> pair_int; std::queue<pair_int> q; bool visit[200001]; int main() { int s, e; std::cin >> s >> e; if (s < e) { q.push(pair_int(s, 0)); while (q.front().first != e) { pair_int tq = q.front(); if (tq.first < e && !visit[tq.first * 2]) q.push(pair_int(tq.first * 2, tq.second + 1)), visit[tq.first * 2] = true; if (tq.first < e && !visit[tq.first + 1]) q.push(pair_int(tq.first + 1, tq.second + 1)), visit[tq.first + 1] = true; if (tq.first > 0 && !visit[tq.first - 1]) q.push(pair_int(tq.first - 1, tq.second + 1)), visit[tq.first - 1] = true; q.pop(); } std::cout << q.front().second; } else std::cout << s - e; return 0; }