Hanoi
递归解决 汉诺塔
class Han{
int num;
int steps;
Han(int num){
this.num=num;
}
void total()
{
System.out.println("total="+steps);
}
void show(int i, int from, int des, String[] str)
{
int tmp = 3-from-des;//0+1+2=3
if (i==2)
{
System.out.println(1+":"+str[from]+"->"+str[tmp]);
System.out.println(2+":"+str[from]+"->"+str[des]);
System.out.println(1+":"+str[tmp]+"->"+str[des]);
steps+=3;
}else
{
show(i-1, from, tmp, str);
System.out.println(i+":"+str[from]+"->"+str[des]);
show(i-1, tmp, des, str);
steps+=1;
}
}
}
public class HelloWorld{
public static void main(String[] args) {
Han h=new Han(5);//set numbers of member
String[] str= {"A","B","C"};//three pillars
h.show(h.num, 0, 2, str);//from pillar 0 to pillar 2
h.total();//2^n-1
}
}
Hanoi的更多相关文章
- Hanoi问题java解法
用什么语言解法都差不多,思路都是一样,递归,这其中只要注重于开始和结果的状态就可以了,对于中间过程,并不需要深究.(我细细思考了一下,还是算了.=_=) 代码其实很简单注重的是思路. 问题描述:有一个 ...
- HDU1329 Hanoi Tower Troubles Again!——S.B.S.
Hanoi Tower Troubles Again! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- ZOJ-1239 Hanoi Tower Troubles Again!
链接:ZOJ1239 Hanoi Tower Troubles Again! Description People stopped moving discs from peg to peg after ...
- Hanoi问题
#include<stdio.h>int main(){ int m; void hanoi(int n,char x,char y,char z); printf("input ...
- The Towers of Hanoi Revisited---(多柱汉诺塔)
Description You all must know the puzzle named "The Towers of Hanoi". The puzzle has three ...
- Hanoi塔
2016-03-19 17:01:35 问题描述: 假设有三个命名为 A B C 的塔座 ,在塔座A上插有n个直径大小不相同,由小到大编号为1 ,2 ,3 ,··· ,n的圆盘,要求将A座上的圆盘移至 ...
- [CareerCup] 3.4 Towers of Hanoi 汉诺塔
3.4 In the classic problem of the Towers of Hanoi, you have 3 towers and N disks of different sizes ...
- 栈应用hanoi
/* 课本p54页*/ #include<stdio.h> #include <iostream> using namespace std; void move(int n, ...
- 【数据结构】hanoi
#include<stdio.h> void hanoi(int n,char x,char y,char z) { ; ) printf("%d. Move disk %d f ...
- 算法训练 Hanoi问题
算法训练 Hanoi问题 时间限制:1.0s 内存限制:512.0MB 问题描述 如果将课本上的Hanoi塔问题稍做修改:仍然是给定N只盘子,3根柱子,但是允许每次最多移动相邻的 ...
随机推荐
- spring+ibatis环境搭建
简单的spring+ibatis入门实例:ibatis是一种半自动化的持久层框架,它介于JDBC和hibernate之间,使用比较灵活. 一:目录结构 二:需要导入的jar包: 所有的第三方jar包都 ...
- .Net 组件技术概述
1. 基本原理 组件是组件系统中功能的表现,没有组件就没有功能.特定接口是用于给组件管理程序来操纵.管理该组件,特定功能是组件需要完成的任务.在一个使用组件建立的产品中会随着功能数目的多少而会有多个组 ...
- Latex之CJK中文书签乱码[转]
在使用CJK宏包编辑中文时,可以使用不同的编码方式,一种是UTF-8格式:另外一种是GBK编码.在使用GBK编码时,生成的PDF格式中的文本是GBK的编码方式,因此大部分不支持GBK编码的PDF阅读器 ...
- asp.net导入2013版本的excel问题解决
net中导入2013excel的故障解决办法. 修改导入excel的连接字符串 string strCon = "Provider=Microsoft.ACE.OLEDB.12.0;Data ...
- C# TcpClient 连接状态检测
C# TcpClient在连接成功后无法检测连接状态,即使对方关闭了网络连接.以下扩展可检测连接状态: public static class TcpClientEx { public static ...
- 使用 vmstat 监测系统性能
在linux/unix下,vmstat是常用的系统性能监测工具.常用用法如下 vmstat 1 10 表示以1秒为间隔,做相关参数的采样,一共10次.输出范例如下: procs ----------- ...
- Python的对象操作(一)
python支持对象和函数 1. python是解释型语言,逐行运行 2. 对象调用 例子:删除文件的一个例子 2.1 先定义一个类 class MyApp: 2.2 import 引用要用到的模 ...
- Java实战之04JavaWeb-04JSP、EL表达式、JSTL标签库
一.jsp部分 只要是与页面显示相关的都是重点 1.jsp的脚本 <%java代码%>:被翻译到service方法内部,局部变量,局部的功能 <%=表达式或变量%>:翻译成se ...
- HDU 1069 Monkey and Banana(动态规划)
Monkey and Banana Problem Description A group of researchers are designing an experiment to test the ...
- XML, XPath, Xslt及解析/Parse
XML及解析/Parse "Programming with libxml2 is like the thrilling embrace of an exotic stranger.&quo ...