Programming/Baekjoon

백준 1463 1로만들기 [c++]

fishersheep 2022. 2. 17. 21:43
반응형
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>

using namespace std;

int n;


void bfs(int x)
{
	queue<pair<int, int>>q;
	q.push({x,0});

	while (!q.empty())
	{
		int a = q.front().first;	//현재값 저장
		int c = q.front().second;	//연산회수를 저장
		q.pop();


		if (a == 1)	//1이되면 결과출력 및 종료
		{
			cout << c;
			return;
		}

		if (a < 0 || a>1000000)continue;	//범위를 벗어나면 continue
		
		if (a % 3 == 0)	//3으로 나누는 경우
			q.push({ a/3,c + 1 });
		
		if (a % 2 == 0)	//2로 나누는 경우
			q.push({ a / 2,c + 1 });
		
		if (a > 1)	//-1을 하는 경우
			q.push({ a - 1,c + 1 });

	}

}

int main()
{
	cin >> n;
	bfs(n);
	return 0;
}
반응형

'Programming > Baekjoon' 카테고리의 다른 글

백준 2212 센서 [c++]  (0) 2022.02.19
백준 5585 거스름돈 [c++]  (0) 2022.02.19
백준 9095 1,2,3더하기 [c++]  (0) 2022.02.16
백준 15652 N과M (4) [c++]  (0) 2022.02.16
백준 1697 숨바꼭질 [c++]  (0) 2022.02.16