汉诺塔问题(递归与非递归)

汉诺塔问题

基本介绍

汉诺塔是由三根杆子A,B,C组成的。A杆上有N个(N>1)穿孔圆盘,盘的尺寸由下到上依次变小。要求按下列规则将所有圆盘移至C杆:每次只能移动一个圆盘;大盘不能叠在小盘上面。提示:可将圆盘临时置于B杆,也可将从A杆移出的圆盘重新移回A杆,但都必须尊循上述两条规则。问:如何移?最少要移动多少次?
在这里把规则加强一下,A和C之间不能直接移动,需要移动到B,再从B移动到A或C。

递归方法

这里就直接贴上代码,基本每句都有注释。

public int hanoiProblem1(int num, String left, String mid, String right) {
		if (num < 1) {
			return 0;
		}
		return process(num, left, mid, right, left, right);
	}

	public static int process(int num, String left, String mid, String right, String from, String to) {
		if (num == 1) {
			if (from.equals(mid) || to.equals(mid)) {
				System.out.println("Move 1 from " + from + " to " + to);  //起点或终点是中间的柱子,则直接移动,次数返回1
				return 1;
			} else {
				System.out.println("Move 1 from " + from + " to " + mid); //起点或者终点是左和右的话,先移动到中间柱子上,再移动到终点上
				System.out.println("Move 1 from " + mid + " to " + to); //次数返回2
				return 2;
			}
		}
		if (from.equals(mid) || to.equals(mid)) {
			String another = (from.equals(left) || to.equals(left)) ? right : left; //终点或者起点要是mid的话,找出不是终点也不是起点的另一个柱子another
			int part1 = process(num - 1, left, mid, right, from, another);	//将n-1个从from移到another上
			int part2 = 1;
			System.out.println("Move " + num + " from " + from + " to " + to); //将第n个从from移动到to
			int part3 = process(num-1, left, mid, right, another, to);  //将n-1个从another移动到to
			return part1 + part2 + part3;
		} else {
			int part1 = process(num - 1, left, mid, right, from, to);  //将n-1个从from移动到to上
			int part2 = 1;
			System.out.println("Move " + num + " from " + from + " to " + mid); //将第n个从from上移动到mid上
			int part3 = process(num-1, left, mid, right, to, from);  //将第n-1个从to移动到from上
			int part4 = 1;
			System.out.println("Move " + num + " from " + mid + " to " + to); //将第n个从mid移动到to
			int part5 = process(num - 1, left, mid, right, from, to); //将第n-1个从from移动到to上
			return part1 + part2 + part3 + part4 + part5;
		}
	}

非递归方法

用栈来模拟整个过程,把左中右三个地点抽象成栈,所以整个动作就可以抽象成从某个栈把栈顶元素弹出,然后压入另一个栈中作为另一个栈的栈顶。这其中有两个原则:
1、小压大原则:从from栈弹出的元素num如果要压入to中,那么num的值必须小于当前to栈的栈顶。
2、相邻不可逆:因为要求的是最优解,所以不能有重复的可逆操作,比如从from弹出到mid,然后再把mid弹出到from,这样会凭空增加次数。
然后根据这两个原则,可以推出两个结论:
1、游戏的第一步一定是从左到中
2、在走出最少步数过程中的任何时刻,四个动作(左到中,中到左,右到中,中到右)中只有一个动作不违反小压大和相邻不可逆原则,另外三个会违反。
代码如下(有注释):

	public enum Action {
		No, LToM, MToL, MToR, RToM  //无动作, 左到中,中到左,中到右,右到中
	}

	public int hanoiProblem2(int num, String left, String mid, String right) {
		Stack<Integer> lS = new Stack<>();
		Stack<Integer> mS = new Stack<>();
		Stack<Integer> rS = new Stack<>();  //左,中,右三个栈
		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 += fStackTotStack(record, Action.MToL, Action.LToM, lS, mS, left, mid);
			step += fStackTotStack(record, Action.LToM, Action.MToL, mS, lS, mid, left);
			step += fStackTotStack(record, Action.RToM, Action.MToR, mS, rS, mid, right);
			step += fStackTotStack(record, Action.MToR, Action.RToM, rS, mS, right, mid);
		}
		return step;
	}

	public static int fStackTotStack(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 " + from + " to " + to);
			record[0] = nowAct;
			return 1;
		}
		return 0;

	}

这是从左程云左神的书上所学的知识的总结。才疏学浅,若有错误欢迎大家多多提出,定虚心接受。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值