알고리즘

C++ 알고리즘 - 백준 11729 하노이 탑 이동순서 (재귀함수)

마루설아 2025. 1. 17. 21:25

https://www.acmicpc.net/problem/11729

 

#include <bits/stdc++.h>
#define endl "\n"

using namespace std;

void CPP_INIT() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
}

void hanoi(int n, int from, int by, int to) {
	if (n == 1) {
		cout << from << " " << to << endl;
	}

	else {
		hanoi(n - 1, from, to, by);
		cout << from << " " << to << endl;
		hanoi(n - 1, by, from, to);
	}
}

int main(void) {
	CPP_INIT();

	int input;
	cin >> input;

	cout << int(pow(2, input)) - 1 << endl;
	hanoi(input, 1, 2, 3);
}