[백준] 11729번 하노이탑 이동순서 c++

semi·2022년 2월 3일
0

coding test

목록 보기
12/57
#include <iostream>
using namespace std;

void hanoi(int n, int start, int to, int bypass)
{
	if (n == 1)
	{
		cout << start << " " << to << "\n";
	}
	else
	{
		hanoi(n - 1, start, bypass, to);
		cout << start << " " << to << "\n";
		hanoi(n - 1, bypass, to, start);
	}
}

int main(void)
{
	int num;
	cin >> num;
	cout << (1<<num) - 1 << "\n";
	hanoi(num, 1, 3, 2);
	return 0;
}

#참고 : https://cryptosalamander.tistory.com/39

0개의 댓글