승쨩개발공부

[BJ] 1697 - 숨바꼭질 (BPS 1차원 최단거리 변형) 본문

CodingTestTraining/BaekJoon(Basic)

[BJ] 1697 - 숨바꼭질 (BPS 1차원 최단거리 변형)

SeungHyune 2025. 10. 28. 12:10
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;


int n, k;
int vis[100005];

int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

	cin >> n >> k;

	queue<int> q;

	vis[n] = 1;
	q.push(n);

	while (q.size())
	{
		int now = q.front();
		q.pop();

		for (int next : {now + 1, now - 1, now * 2})
		{
			if (next < 0 || next > 100000) continue;
			if (vis[next]) continue;

			q.push(next);
			vis[next] = vis[now] + 1;
		}
	}

	cout << vis[k] - 1;


	return 0;
}