Chinaunix首页 | 论坛 | 博客
  • 博客访问: 258892
  • 博文数量: 70
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 930
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 12:32
文章存档

2009年(42)

2008年(28)

我的朋友

分类:

2008-11-01 10:23:08

 exp: 有3个塔座x y z,在x上插有n个直径大小各不相同、从小到大编号为1 - n的圆盘,要求将x轴上的n个圆盘移动到z轴并按同样顺序排列,移动圆盘须遵循以下规则:
 1)、每次只能移动一个圆盘;
 2)、圆盘可插在x y z中的任一塔座上;
 3)、任何时刻不能将一个较大的圆盘压在较小的圆盘上;

 ADT - abstract data type

采用递归方式实现,具体实现时要用到堆栈
 void hanoi(int n,char x,char y,char z){
 // 将x上的n个圆盘移动到z上,y作为辅助塔座
 if n==1
    move(1,x,z)
 else {
    hanoi(n-1,x,z,y)
    //将x上的n-1个圆盘移动到y上,z作为辅助塔座,
    move(n,x,z)
    //将第n个圆盘从x移动到z
    hanoi(n-1,y,x,z)
    //将y上编号为1 ~ n-1的圆盘移动到z,x作为辅助塔座
   }
 }

------hanoi 河内,capital of Vietnam

------ 注:
The famous problem of Hanoi-Tower always appears in many branches of the science of computer. But whether there is the non-recursive algorithm for this problem has ever been a puzzle. In this paper, the author advanced two non-recursive algorithms, the equivalent word algorithm and state-diagram algorithm, which are particula- rly suitable for solving combination--pattern recursive relations. He applied these non-recursive algorithms to the problem of Hanoi--Tower and attained his success in deducing thc p...
阅读(1716) | 评论(0) | 转发(0) |
0

上一篇:关于session

下一篇:Rules of Snooker

给主人留下些什么吧!~~