Hanoi塔问题

//-----------------------Hanoi.cpp-----------------------------
#include<iostream>
#include<iomanip>
using namespace std;

void move(unsigned n,unsigned & moveNumber,char sourse,char destination,char spare);
int main()
{
	const char PEG1='A',
	           PEG2='B',
			   PEG3='C';
	unsigned moveNumber=0;
	
	cout<<"This program solves the Hanio Towers puzzle.\n\n";
	cout<<"Enter the number of disks: ";
	int numDisks;
	cin>>numDisks;
	cout<<endl;
	move(numDisks,moveNumber,PEG1,PEG2,PEG3);
	system("pause");
}

void move(unsigned n,unsigned & moveNumber,char source,char destination,char spare)
{
	if(n==1)
	{
		moveNumber++;
		cout<<setw(3)<<moveNumber
			<<". Move the top disk from "<<source
			<<" to "<<destination<<endl;
	}
	else
	{
		move(n-1,moveNumber,source,spare,destination);
		move(1,moveNumber,source,destination,spare);
		move(n-1,moveNumber,spare,destination,source);
	}	
}

This program solves the Hanio Towers puzzle.

Enter the number of disks: 4

  1. Move the top disk from A to C
  2. Move the top disk from A to B
  3. Move the top disk from C to B
  4. Move the top disk from A to C
  5. Move the top disk from B to A
  6. Move the top disk from B to C
  7. Move the top disk from A to C
  8. Move the top disk from A to B
  9. Move the top disk from C to B
 10. Move the top disk from C to A
 11. Move the top disk from B to A
 12. Move the top disk from C to B
 13. Move the top disk from A to C
 14. Move the top disk from A to B
 15. Move the top disk from C to B
请按任意键继续. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值