유명한 하노이 문제이다.

재귀로 푸는게 일반적인 방법인것 같다.

#include <string>
#include <vector>

using namespace std;

vector<vector<int>> hanoi;

void Hanoi(int start, int end, int n) {
    if (n == 1) 
    {
        hanoi.push_back({ start, end });
        return;
    }

    Hanoi(start, 6 - start - end, n - 1);
    hanoi.push_back({ start, end });
    Hanoi(6 - start - end, end, n - 1);
}

vector<vector<int>> solution(int n) {
    vector<vector<int>> answer;

    Hanoi(1, 3, n);
    
    answer = hanoi;

    return answer;
}

+ Recent posts