hanio塔的递归与非递归算法

首先是递归算法 就是设法让A塔上的n-1个先移动到C塔上 再把最下面的那个移动到B塔上 然后再将C塔上的n-1个移动到B塔上。

#include <iostream>
using namespace std;

void move(char x,int n,char y)
{
	cout<<"move disk "<<n<<" from "<<x<<" to "<<y<<endl;
}

void Hanio(int n,char x,char y,char z)
{
	//将x塔上的n个移动到y塔上
	if(n==1)
		move(x,1,y);
	else
	{
		Hanio(n-1,x,z,y);
		move(x,n,y);
		Hanio(n-1,z,y,x);
	}
}

int main()
{
	char x='x',y='y',z='z';
	Hanio(3,x,y,z);
	cout<<"Finished !"<<endl;
	return 0;
}

测试结果:

move disk 1 from x to y
move disk 2 from x to z
move disk 1 from y to z
move disk 3 from x to y
move disk 1 from z to x
move disk 2 from z to y
move disk 1 from x to y
Finished !
Press any key to continue

非递归代码:

这个是要移动A到C的

#include <iostream>
using namespace std;

const int MAX_SPACE=64;

struct st
{
	int s[MAX_SPACE];
	char name;
	int top;
	
	int Top()
	{
		return s[top];
	}

	int pop()
	{
		return s[top--];
	}

	void push(int x)
	{
		s[++top]=x;
	}
};


long pow(int x,int y)
{
	//计算x^y
	long sum=1;
	for(int i=0;i<y;i++)
		sum*=x;
	return sum;
}

void Create(st ta[],int n)
{
	ta[0].name='A';
	ta[0].top=n-1;
	//把所有的圆盘从大到小放到A上
	for(int i=0;i<n;i++)
		ta[0].s[i]=n-i;
	//把柱子B,C开始没有圆盘
	ta[1].top=ta[2].top=0;
	for(i=0;i<n;i++)
		ta[1].s[i]=ta[2].s[i]=0;
	//若n为偶数,则按顺时针一次摆放A B C
	if(n%2==0)
	{
		ta[1].name='B';
		ta[2].name='C';
	}
	else //若n为奇数,按顺时针摆放A C B
	{
		ta[1].name='C';
		ta[2].name='B';
	}
}

void Hanio(st ta[],long max)
{
	int k=0;//累计移动的次数
	int i=0;
	int ch;
	while(k<max)
	{
		//按顺时针方向把圆盘1从现在的柱子移动到下一根柱子
		ch=ta[i%3].pop();
		ta[(i+1)%3].push(ch);
		cout<<++k<<": Move disk "<<ch<<" from "<<ta[i%3].name
			<<" to "<<ta[(i+1)%3].name<<endl;
		i++;
		//把非空柱子上的圆盘移动到空柱子上,当两柱子都空的时候,移动较小的圆盘
		if(k<max)
		{
			if(ta[(i+1)%3].Top()==0 || ta[(i-1)%3].Top()>0
				&& ta[(i+1)%3].Top()>ta[(i-1)%3].Top())
			{
				ch=ta[(i-1)&3].pop();
				ta[(i+1)%3].push(ch);
				cout<<++k<<": Move disk "<<ch<<" from "<<ta[(i-1)%3].name
					<<" to "<<ta[(i+1)%3].name<<endl;
			}
			else
			{
				ch=ta[(i+1)%3].pop();
				ta[(i-1)%3].push(ch);
				cout<<++k<<": Move disk "<<ch<<" from "<<ta[(i+1)%3].name
					<<" to "<<ta[(i-1)%3].name<<endl;
			}
		}
	}
}
/*-------------------------------------------------
/ 首先把三根柱子摆成品字形,把所以的圆盘从大到
/ 小放到A上,根据圆盘的数量确定柱子的摆放顺序,若
/ n为偶数按顺时针摆放A B C否则按顺时针摆放A C B。
/ (1)按顺时针把圆盘1从现在的柱子移动到下一根柱子
/ 就是n为偶数的时候就按照A->B>C,为奇数就按照
/ A->C>B的顺序(2)把另外两根柱子上可以移动的圆盘移
/ 动到新的柱子上,即把非空柱子上的圆盘移动到空柱子
/ 上,当两根柱子都非空时,移动较小的圆盘,这一步
/ 实施的行动是唯一的(3)反复进行(1)(2)操作
/--------------------------------------------------*/

#include "hanio.h"

int main()
{
	int n;
	cin>>n;
	st ta[3];
	Create(ta,n);

	long max=pow(2,n)-1;
	Hanio(ta,max);

	return 0;
}

测试结果:

3
1: Move disk 1 from A to C
2: Move disk 2 from A to B
3: Move disk 1 from C to B
4: Move disk 3 from A to C
5: Move disk 1 from B to A
6: Move disk 2 from B to C
7: Move disk 1 from A to C
Press any key to continue

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值