https://www.acmicpc.net/problem/1697
#include <iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int FindfastetPath(int n, int k)
{
const int MAX = 100000;
queue<pair<int, int>> now;
vector<bool> visited(MAX+1, false);
now.push({ n, 0 });
visited[n] = true;
while (!now.empty())
{
int current = now.front().first;
int time = now.front().second;
now.pop();
if (current == k) return time;
if (current - 1 >= 0 && !visited[current - 1])
{
now.push({ current - 1, time + 1 });
visited[current - 1] = true;
}
if (current + 1 <= MAX && !visited[current + 1])
{
now.push({ current + 1, time + 1 });
visited[current + 1] = true;
}
if (current * 2 <= MAX && !visited[current * 2])
{
now.push({ current * 2, time + 1 });
visited[current * 2] = true;
}
}
return -1;//만약 K에 도달하지 못하면...
}
int main()
{
int n, k;
cin >> n >> k;
cout << FindfastetPath(n, k) << endl;
return 0;
}
백준 1707 // (0) | 2025.01.23 |
---|---|
백준 2667 //단지번호붙이기 (0) | 2025.01.22 |
백준 1260번 //DFS와 BFS// (0) | 2025.01.20 |
백준 // 2470//두 용액/ 항해99//정렬과 투 포인터(Two Pointers) 알고리즘 (0) | 2025.01.19 |
백준 //선분 위의 점//11663//정렬(Sorting)**과 **이분 탐색(Binary Search) (0) | 2025.01.19 |