Hanoi

December 12, 2012


#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

int step = 1;
void move(char from, char to)
{
    printf("%d: %c->%c\n", step++, from, to);
}
void h(int n, char from, char via, char to)
{
    if (n == 1) {move(from, to); return;}
    h(n-1, from, to, via);
    move(from, to);
    h(n-1, via, from, to);
}
int main(void)
{
    int n;
    while (cin >> n)
    {
        step = 1;
        h(n, 'x', 'y', 'z');
    }

    return 0;
}

挺简单的一个Hanoi程序

comments powered by Disqus