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根柱子,但是允许每次最多移动相邻的 ...
随机推荐
- MySQL创建/删除/清空表,添加/删除字段
创建表: create table tablename (column_name column_type); create table table_name( id int not null auto ...
- 局域网内sqldeveloper客户端连接oracle服务器
1:输入参数 输入连接名(随便写) 用户名:system/tiger/sys或者是创建的用户 口令:设置的口令 主机名:连接的数据库所在的ip 端口:默认是1521 sid:orcl安装时自己设置的 ...
- Linux VIM python 自动补全插件:pydiction
Pydiction 可以是我们使用Tab键自动补全Python代码在Vim,是一款非常不错的插件. Pydiction不需要安装,所有没有任何依赖包问题,Pydiction主要包含三个文件. pyth ...
- xxx is not in the sudoers file.This incident will be reported.的解决方法
1.切换到root用户下,怎么切换就不用说了吧,不会的自己百度去. 2.添加sudo文件的写权限,命令是:chmod u+w /etc/sudoers 3.编辑sudoers文件vi /etc/sud ...
- DOS批处理命令-echo
Echo 命令 打开回显或关闭请求回显功能,或显示消息.如果没有任何参数,echo 命令将显示当前回显设置. 语法 echo [{on off}] [message] 即是说当echo设置off值 ...
- HW--自守数
package testcase; import huawei.Demo; import junit.framework.TestCase;//加入测试框架,不需要写Main函数 public cla ...
- C# TcpClient 连接状态检测
C# TcpClient在连接成功后无法检测连接状态,即使对方关闭了网络连接.以下扩展可检测连接状态: public static class TcpClientEx { public static ...
- Jquery中Ajax异步请求中的async参数的作用
之前不知道这个参数的作用,上网找了前辈的博客,在此收录到自己的博客,希望能帮到更多的朋友: test.html <a href="javascript:void(0)" on ...
- 禁用浏览器缓存Ajax请求
$.ajax({ url: 'url.php', cache: false, success: function(data){ //..... } }); 仅Get有缓存, Post不会缓存
- jquery load
$('#loadFooter').click(function() { $('#footer').load('footer.html'); });