用栈的汉诺塔问题程序

来源:百度知道 编辑:UC知道 时间:2024/05/27 14:06:19
假设有3个分别命名X,Y和Z的塔座,在塔座X上插有n个圆盘。现要求将其移至Z塔座,必须满足规则:
每次只能移动一个圆盘;
圆盘可以插在X,Y和Z中任意个塔座上;
任何时刻都不能将一个较大的圆盘压在较小的圆盘上。

我想学习一下年怎么用栈表达,那位打下贡献一下代码。能让我直接应用的

#include<stdio.h>
#include<iostream.h>
int main()
{
int hanoi(int n,char out,char two, char three);
int m;
cin>>m;
hanoi(m,'A','B','C');
cin.get();cin.get();
}
int hanoi(int n,char one,char two,char three)//将N个盘子从A到C,借助B坐
{ int move(char x,char y);
if(n==1) move (one,three else
{hanoi(n-1,one,three,two);move(one,three);hanoi(n-1,two,one,three }
int move (char x,char y {
cout<<x<<"-->"<<y<<" ";

#include<stdio.h>
int c=0; /* 全局变量,搬动次数*/
void move(char x,int n,char z)
{ /* 第n 个圆盘从塔座x 搬到塔座z */
printf("第%i 步: 将%i 号盘从%c 移到%c\n",++c,n,x,z);
}
void hanoi(int n,char x,cha