汉诺塔(Tower of Hanoi)问题

汉诺塔(Tower of Hanoi)问题

汉诺塔(Tower of Hanoi)问题_第1张图片

#include
using namespace std;

void move(char from, char to)
{
	cout << "from  " << from << "  to  " << to << endl;
}

void hanio(int n, char first, char second, char third)
{
	if (n == 1)
		move(first, third);
	else {
		hanio(n - 1, first, third, second);
		move(first, third);
		hanio(n - 1, second, first, third);
	}
}

int main()
{
	hanio(3, 'a', 'b', 'c');
	return 0;
}

汉诺塔(Tower of Hanoi)问题_第2张图片

你可能感兴趣的:(算法设计与分析)