public class Hano {
String a = "A";
String b = "B";
String c = "C";
int moveCnt;
int hano(String x, String y, String z, int level){
if (1 == level){
System.out.println(x + "==>" + z);
moveCnt++;
} else if (2 == level){
System.out.println(x + "==>" + y);
System.out.println(x + "==>" + z);
System.out.println(y + "==>" + z);
moveCnt+=3;
} else if (level >2){
hano(x,z,y,level-1);
hano(x,y,z,1);
hano(y,x,z,level-1);
}
return moveCnt;
}
int hano(int level){
return hano(a,b,c,level);
}
public static void main(String args[]){
int level = 5; // you can change the level here
Hano h = new Hano();
System.out.println("Totally moved: " + h.hano(level) + "times");
}
}
String a = "A";
String b = "B";
String c = "C";
int moveCnt;
int hano(String x, String y, String z, int level){
if (1 == level){
System.out.println(x + "==>" + z);
moveCnt++;
} else if (2 == level){
System.out.println(x + "==>" + y);
System.out.println(x + "==>" + z);
System.out.println(y + "==>" + z);
moveCnt+=3;
} else if (level >2){
hano(x,z,y,level-1);
hano(x,y,z,1);
hano(y,x,z,level-1);
}
return moveCnt;
}
int hano(int level){
return hano(a,b,c,level);
}
public static void main(String args[]){
int level = 5; // you can change the level here
Hano h = new Hano();
System.out.println("Totally moved: " + h.hano(level) + "times");
}
}
{
void Move(char chSour,char chDest){
System.out.println("Move the top plate of "+chSour+"-->"+chDest);
}
void Hanoi(int n,char chA,char chB,char chC)
{
if(n==1)
Move(chA,chC);
else
{
Hanoi(n-1,chA,chC,chB);
this.Move(chA,chC);
Hanoi(n-1,chB,chA,chC);
}
}
public static void main(String[] args)
{
int n=Integer.parseInt(args[0]);
HanoiY han=new HanoiY();
han.Hanoi(n,'A','B','C');
}
}
/**
* 双色汉罗塔
* @author neil
* @date 下午2:13:23
* @filename Hanoi2Colors.java
*/
public class Hanoi2Colors {
public static void help() {
System.out.println(
"Usage: java Hanoi2Colors number_of_disks");
System.out.println(
"\t number_of_disks: must be a even number.");
System.exit(0);
}
public static void main(String[] args) {
int disks = 5;
hanoi2colors(disks);
}
public static void hanoi(int disks,
char source, char temp, char target) {
if (disks == 1) {
System.out.println("move disk from "
+ source + " to " + target);
System.out.println("move disk from "
+ source + " to " + target);
} else {
hanoi(disks-1, source, target, temp);
hanoi(1, source, temp, target);
hanoi(disks-1, temp, source, target);
}
}
public static void hanoi2colors(int disks) {
char source = 'A';
char temp = 'B';
char target = 'C';
for (int i = disks / 2; i > 1; i--) {
//将i-1层全部移动到目标柱子
hanoi(i-1, source, temp, target);
//然后将第i层移动到临时柱子
System.out.println("move disk from "
+ source + " to " + temp);
System.out.println("move disk from "
+ source + " to " + temp);
//将i-1层从目标柱子,通过临时柱子(因为临时柱子上面是最大的盘子,因此可以忽略掉)移动到原柱子
hanoi(i-1, target, temp, source);
//将临时柱子上面的盘子分开
System.out.println("move disk from "
+ temp + " to " + target);
}
System.out.println("move disk from "
+ source + " to " + temp);
System.out.println("move disk from "
+ source + " to " + target);
}
}
public static void hanoi(int n, char x, char y, char z) {
if (n == 1) {
System.out.println("将" + n + "层" + x + "移动到" + z);
} else {
// 先将1到n-1层从x经过z移动到y
hanoi(n - 1, x, z, y);
// 将n从x移动到z
System.out.println("将" + n + "层从" + x + "移动到" + z);
// 将1到n-1层从y经过x移动到z
hanoi(n - 1, y, x, z);
}
}