一个汉诺塔的移动过程
public class Test {
public static void main(String[] args){
Test t = new Test();
t.hanoi(2);
}
public void hanoi(int n) {
if (n > 0) {
func(n, "left", "mid", "right");
}
}
public void func(int n, String from, String mid, String to) {
if (n == 1) {
System.out.println("move from " + from + " to " + to);
} else {
func(n - 1, from, to, mid);
func(1, from, mid, to);
func(n - 1, mid, from, to);
}
}
}
显示移动棋子名称
public static void Han(int i,String from,String to,String mid) {
if(i==1) {
System.out.println("move 1 from "+from+" to "+to);
return;
}
Han(i-1,from,mid,to);
System.out.println("move "+i+" from "+from+" to "+to);
// Han(1,from,to,mid);
Han(i-1,mid,to,from);
}