프로그래머스/2레벨
하노이의 탑/C++
Koalitsiya
2023. 2. 3. 15:46
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이방법
제목 그대로 재귀 알고리즘 중 유명한 하노이의 탑을 구현하면 된다.
#include <string>
#include <vector>
using namespace std;
vector<vector<int>> answer;
void Hanoi(int n, int from, int by, int to) {
if (n == 1) answer.push_back({from, to});
else {
Hanoi(n - 1, from, to, by);
answer.push_back({from, to});
Hanoi(n - 1, by, from, to);
}
}
vector<vector<int>> solution(int n) {
Hanoi(n, 1, 2, 3);
return answer;
}