hanoi漢諾塔C++實現

2puT發表於2016-09-17
#include<iostream>
using namespace std;

void hanoi(int n, char one, char two, char three); //Hanoi move
void move(char x, char y); //move step after

int main()
{
    int m;
    cout << "please input num:  ";
    cin >> m;
    hanoi(m,'A','B','C');
    return 0;
}

//Hanoi move
void hanoi(int n, char one, char two, char three)
{
    if(n == 1)
	move(one, three);
    else 
	{
	    hanoi(n-1, one, three, two);
	    move(one, three);
	    hanoi(n-1, two, one, three);
	}
}

//move step after
void move(char x, char y)
{
    cout << x << "-->" << y << endl;
}

相關文章