기록공간

하노이탑(백준-11729) 본문

Algorithm/문제

하노이탑(백준-11729)

입코딩 2019. 7. 26. 11:31
반응형
#include <iostream>
#include <math.h>
using namespace std;

class CHanoi
{
public:
	int GetCount() { return m_nCount; }
public:
	void InputCount()
	{
		cin >> m_nCount;
	}
	void Solve(int circleCount, int from, int by, int to)
	{
		if (circleCount == 1)
		{
			Move(from, to);
		}
		else
		{
			// 1번째에서 3번째를 거쳐 2번째로 n-1만큼 옮긴다.
			// 나머지 하나를 1번쨰에서 3번째로 옮긴다.
			// 2번째에 있는 n-1개를 1번째를 거쳐 3번째로 옮긴다.
			Solve(circleCount - 1, from, to, by);
			Move(from, to);
			Solve(circleCount - 1, by, from, to);
		}
			
	}
	void Move(int from, int to)
	{
		cout << from << ' ' << to << endl;
	}
	void PrintResultCount(int circleCount)
	{
		cout << (int)pow(2, circleCount) - 1 << endl;
	}
private:
	int m_nCount = 0;
};

int main()
{
	CHanoi h;
	h.InputCount();
	h.PrintResultCount(h.GetCount());
	h.Solve(h.GetCount(), 1, 2, 3);
}
반응형

'Algorithm > 문제' 카테고리의 다른 글

로프 (백준 - 2217번)  (0) 2020.02.08
회의실배정 (백준 - 1931번)  (0) 2020.02.08
동전 0 (백준 - 11047번)  (0) 2020.02.08
ATM (백준 - 11399번)  (0) 2020.02.08
별찍기 - 10 (백준-2447)  (0) 2019.07.26
Comments