PS

[BOJ] 31248번 3+1 하노이 탑 C++

Eastplanet 2024. 1. 22. 22:29
#include <iostream>
#include <string>
#include <vector>

using namespace std;

typedef pair<char, char>P;
int moveCount = 0;
vector<P> v;

void move(char start, char end) {
    v.push_back(P(start, end));
    moveCount++;
}

void hanoi(int n, char now, char next, char tmp) {
    if (n == 1) {
        move(now, next);
        return;
    }
    hanoi(n - 1, now, tmp, next);
    move(now, next);
    hanoi(n - 1, tmp, next, now);
}

void modHanoi(int n, char now, char next, char tail, char dest) {
    if (n == 1) {
        move(now, dest);
        return;
    }
    else if(n == 2){
        move(now, next);
        move(now, dest);
        move(next, dest);
        return;
    }
    hanoi(n - 2, now, next, tail);
    move(now, tail);
    move(now, dest);
    move(tail, dest);
    modHanoi(n - 2, next, now, tail, dest);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int N;
    cin >> N;
    modHanoi(N, 'A', 'B', 'C', 'D');
    cout << moveCount << "\n";
    for (P p : v) {
        cout << p.first << " " << p.second << "\n";
    }
    return 0;
}

 

1~N 까지의 탑을 D로 옮기기 위해 modHanoi 함수는 hanoi 함수를 통해 1~N-2까지의 탑을 옮기고

N-1~N의 탑을 D로 옮기는 작업을 수행한다.