hano塔非递归
2013-10-21 16:11
344 查看
#include <stdio.h> const int MaxSize=100; struct { int vn; char x,y,z; int flag; }st[MaxSize]; int top=-1; void hano2(int n,char X,char Y,char Z) { top++; st[top].vn=n; st[top].x=X; st[top].y=Y; st[top].z=Z; st[top].flag=1; while (top>-1) { int vn1; char x1,y1,z1; if (st[top].flag==1) { vn1=st[top].vn; x1=st[top].x; y1=st[top].y; z1=st[top].z; if (st[top].vn==2) { top--; top++; st[top].vn=vn1-1; st[top].x=x1; st[top].y=z1; st[top].z=y1; st[top].flag=0; top++; st[top].vn=1; st[top].x=x1; st[top].y=z1; st[top].z=y1; st[top].flag=0; top++; st[top].vn=vn1-1; st[top].x=y1; st[top].y=x1; st[top].z=z1; st[top].flag=0; } else { top--; top++; st[top].vn=vn1-1; st[top].x=x1; st[top].y=z1; st[top].z=y1; st[top].flag=1; top++; st[top].vn=1; st[top].x=x1; st[top].y=z1; st[top].z=y1; st[top].flag=0; top++; st[top].vn=vn1-1; st[top].x=y1; st[top].y=x1; st[top].z=z1; st[top].flag=1; } } else if (st[top].flag==0) { printf("move %c to %c\n",st[top].x,st[top].z); top--; } } } void main() { char X='X'; char Y='Y'; char Z='Z'; hano2(5,X,Y,Z); }
相关文章推荐
- [Flex] 递归 删除XML 节点
- 一个php从数据库取出的无限极递归多维数组
- 快速排序的非递归实现
- 第4周项目5 (2) 用递归方法求解
- PHP两种实现无级递归分类的方法
- 阶乘的计算-递归思想
- Python学习笔记之递归
- SICP 习题 (1.9) 解题总结:迭代计算过程和递归计算过程
- Java递归
- 第四周项目:用递归调用来求第20个fibnacci
- 递归实现以树的形式输入某一个文件夹下的所有文件及目录,不要求图形界面
- 经典算法——递归求解汉诺塔
- 不用循环和递归判断值在数组中的索引【转载】
- oracle 递归详细解剖
- 矩阵连乘的动态规划算法(包括递归的备忘录方法)
- 递归实现排列组合问题
- java之二叉树的创建和递归遍历
- 面试,递归:买汽水,1块钱可以买1瓶汽水,2个空瓶可以换一瓶汽水,3个瓶盖可以换一瓶汽水,问:20块可以买到多少瓶汽水
- 递归与分治策略——集合划分问题,众数问题
- 杭电2041,超级楼梯,简单递归题目。