汉诺塔非递归解法
来源:互联网 发布:淘宝代理一件商品 编辑:程序博客网 时间:2024/05/15 10:11
#include <iostream>#include <stack>using namespace std;class hanoiunit{public: hanoiunit(int m,char i,char j,char k):n(m),fir(i),sec(j),thi(k){}; hanoiunit(const hanoiunit& b){n=b.n;fir=b.fir;sec=b.sec;thi=b.thi;} int n; char fir,sec,thi;};void hanoi(int,char,char,char);int main(int argc, const char * argv[]) { int m; cin>>m; hanoi(m,'a','b','c'); return 0;}void hanoi(int num,char a,char b,char c){ stack<hanoiunit> s; if(num==1)s.push(hanoiunit(1,a,b,c)); else{ s.push(hanoiunit(num-1, b, a, c)); s.push(hanoiunit(1, a, b, c)); s.push(hanoiunit(num-1, a, c, b)); } while(! s.empty()){ hanoiunit h=s.top(); s.pop(); if(h.n==1)cout<<h.fir<<"->"<<h.thi<<endl; else{ s.push(hanoiunit(h.n-1, h.sec, h.fir, h.thi)); s.push(hanoiunit(1, h.fir, h.sec, h.thi)); s.push(hanoiunit(h.n-1, h.fir, h.thi, h.sec)); } }}
0 0
- 汉诺塔非递归解法
- 汉诺塔非递归解法
- 背包问题,递归解法
- 汉诺塔的递归解法
- 最大公约数的递归解法
- 迷宫 DFS 递归 解法
- 迷宫 DFS 递归 解法
- 汉诺塔递归解法
- 汉诺塔问题递归解法
- 背包非递归解法
- 最大公约数之递归解法
- 九连环-递归解法
- Josephus问题递归解法
- 汉诺斯塔问题递归解法
- Triangle 递归解法
- 递归思想-汉诺塔解法
- 汉诺塔问题递归解法
- Permutation递归解法
- SVN检出项目后报build.properties does not exit完整版解决方案
- AC自动机入门
- HDU 2767 Proving Equivalences(强连通分量)
- Android 进阶 Android 中的 IOC 框架 【ViewInject】
- 使用FMDB操作数据库
- 汉诺塔非递归解法
- C语言——希尔排序
- web开发手册
- KindEditor 添加默认提示信息
- 毕业设计,开题计划
- android studio 查看方法和属性列表
- JVM 的个人见解
- xUtils3的使用
- RtlCopyUnicodeString routine