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) |