程序员代码面试指南第二版 6.用栈来求解汉诺塔问题

welcome to my blog

程序员代码面试指南第二版 6.用栈来求解汉诺塔问题

题目描述

题目描述
汉诺塔问题比较经典,这里修改一下游戏规则:现在限制不能从最左侧的塔直接移动到最右侧,也不能从最右侧直接移动到最左侧,
而是必须经过中间。求当塔有n层的时候,打印最优移动过程和最优移动总步数。

输入描述:
输入一个数n,表示塔层数

输出描述:
按样例格式输出最优移动过程和最优移动总步数

示例1

输入
2

输出
Move 1 from left to mid
Move 1 from mid to right
Move 2 from left to mid
Move 1 from right to mid
Move 1 from mid to left
Move 2 from mid to right
Move 1 from left to mid
Move 1 from mid to right
It will move 8 steps.

说明
当塔数为两层时,最上层的塔记为1,最下层的塔记为2
第一次做, 用栈求解; 基本操作只有四个, left2mid, mid2left, right2mid, mid2right; 在挪动总步数最小的约束下, 每一步都满足两个条件: 相邻不可逆; 小压大; 这两个条件使用栈解决汉诺塔问题的核心; 使用了enum类型
import java.util.Scanner;
import java.util.Stack;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        int res = hanoi(n);
        System.out.println("It will move "+ res +" steps.");
    }
    public static int hanoi(int n){
        Stack<Integer> l = new Stack<>();
        Stack<Integer> m = new Stack<>();
        Stack<Integer> r = new Stack<>();
        //先压入系统最大值, 后面就不用判断栈为空的情况了
        l.push(Integer.MAX_VALUE);
        m.push(Integer.MAX_VALUE);
        r.push(Integer.MAX_VALUE);
        //把盘子都放在左柱上
        for(int i=n; i>=1; i--)
            l.push(i);
        //开始挪动
        int step = 0;
        Action[] pre = {Action.No};
        //因为栈最开始压入了系统最大值, 所以循环终止条件是n+1, 不是n
        while(r.size()!=n+1){
            step += oneStep(pre, Action.mid2left, Action.left2mid,"left", "mid", l, m);
            step += oneStep(pre, Action.left2mid, Action.mid2left,"mid", "left", m, l);
            step += oneStep(pre, Action.mid2right, Action.right2mid,"right", "mid", r, m);
            step += oneStep(pre, Action.right2mid, Action.mid2right,"mid", "right", m, r);
        }
        return step;
    }
    public static enum Action{
        No, left2mid, mid2left, right2mid, mid2right
    }
    //每一步操作都要满足两个约束:  相邻不可逆; 小压大
    public static int oneStep(Action[] pre, Action ifReverse, Action now, String from, String to, Stack<Integer> f, Stack<Integer> t){
        if(pre[0] != ifReverse && f.peek() < t.peek()){
            int curr = f.pop();
            t.push(curr);
            System.out.println("Move " + curr + " from " + from + " to " + to);
            pre[0] = now;
            return 1;
        }
        return 0;
    }
}
第一次做, 感受递归; 当前变量自己处理, 更新后的变量或者说未处理的变量让递归函数处理; 按照是否有"mid"分成两大类进行讨论; 把from和to的各种组合写在纸上就知道oth应该是谁了, 组合的可能很少, 别觉得麻烦不写; 每次都是处理最底下的那个盘子, 上面的盘子用递归函数处理; from或者to中有一方是"mid"的话, 需要3个流程从from到达to; from或者to都不是"mid"的话, 需要5个流程从from到达to
  • 每次都是处理最底下的盘子, 上面的盘子用递归函数处理
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());
        //input check
        if(n<1)
            return;

        int res = hanoi(n, "left", "right");
        System.out.println("It will move "+ res +" steps.");
    }

    public static int hanoi(int n, String from, String to){
        //base case
        if(n==1){
            if(from.equals("mid") || to.equals("mid")){
                System.out.println("Move " + n + " from " + from + " to " + to);
                return 1;
            }
            else{
                System.out.println("Move " + n + " from " + from + " to " + "mid");
                System.out.println("Move " + n + " from " + "mid" + " to " + to);
                return 2;
            }
        }
        //
        if(from.equals("mid") || to.equals("mid")){
            String oth = from.equals("mid") ? (to.equals("left")? "right" : "left") : (from.equals("left")?"right":"left");
            int part1 = hanoi(n-1, from, oth);
            System.out.println("Move " + n + " from " + from + " to " + to);
            int part3 = hanoi(n-1, oth, to);
            return part1 + 1 + part3;
        }
        else{

            int part1 = hanoi(n-1, from, to);
            System.out.println("Move " + n + " from " + from + " to " + "mid");
            int part3 = hanoi(n-1, to, from);
            System.out.println("Move " + n + " from " + "mid" + " to " + to);
            int part5 = hanoi(n-1, from, to);
            return part1 + part3 + part5 + 2;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值