diff --git a/src/week3/BOJ17071/BOJ17071_V2.cpp b/src/week3/BOJ17071/BOJ17071_V2.cpp new file mode 100644 index 0000000..f4b5c27 --- /dev/null +++ b/src/week3/BOJ17071/BOJ17071_V2.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; +int bro, subin; +int visited[2][500001]; +int main() { + + ios_base::sync_with_stdio(false); + cin.tie(NULL); + + cin >> subin >> bro; + + queue q; + q.push(subin); + visited[0][subin] = 1; + int t = 0; + while(!q.empty()) { + int size = q.size(); + int bro_pos = t * (t + 1) / 2 + bro; + if (bro_pos > 500000) break; + if (visited[t%2][bro_pos]) { + cout << t; + return 0; + } + + while(size--) { + int curr = q.front(); + q.pop(); + + for (int next : {curr+1, curr-1, curr*2}) { + if (next < 0 || next > 500000) continue; + if (visited[(t+1)%2][next]) continue; + visited[(t+1)%2][next] = 1; + q.push(next); + } + } + t++; + } + + cout << -1; + + return 0; +} \ No newline at end of file