import java.util.Stack;
class Demo{
public enum Action{
No,LToM,MToL,MToR,RToM
}
//num是盘子的数量,left,mid,right分别代表左中右三个柱子
public static int hanoi(int num,String left,String mid,String right){
//lS,mS,rS代表左中右三个栈(模拟柱子)
Stack<Integer> lS = new Stack<Integer>();
Stack<Integer> mS = new Stack<Integer>();
Stack<Integer> rS = new Stack<Integer>();
lS.push(Integer.MAX_VALUE);
mS.push(Integer.MAX_VALUE);
rS.push(Integer.MAX_VALUE);
for(int i=num;i>0;i--){
lS.push(i);
}
Action[] record = { Action.No };
int step = 0;
while(rS.size() != num+1){
step += fStackToStack(record,Action.MToL,Action.LToM,lS,mS,left,mid);
step += fStackToStack(record,Action.LToM,Action.MToL,mS,lS,mid,left);
step += fStackToStack(record,Action.MToR,Action.RToM,rS,mS,right,mid);
step += fStackToStack(record,Action.RToM,Action.MToR,mS,rS,mid,right);
}
return step;
}
//preNoAct是与现在所要进行的动作相反的动作,nowAct是现在所要进行的动作
public static int fStackToStack(Action[] record,Action preNoAct,Action nowAct,Stack<Integer> fStack,Stack<Integer> tStack,String from,String to){
if(record[0] != preNoAct && fStack.peek() < tStack.peek()){
tStack.push(fStack.pop());
System.out.println("Move " + tStack.peek() + " " + from + "->" + to);
record[0] = nowAct;
return 1;
}
return 0;
}
public static void main(String[] args){
int i = hanoi(3,"left","mid","right");
System.out.println("一共走了" + i + "步");
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有