PanheadGG
PanheadGG
发布于 2025-12-27 / 128 阅读

数据结构实验五:综合实验之汉诺塔问题

题目

1.使用递归实现汉诺塔问题。

2.使用栈改写汉诺塔问题,不能使用递归。输出栈的运行情况。

代码

递归实现

import java.util.Stack;

public class HanoiTower {
    public static final int SRC = 0;
    public static final int TGT = 1;
    public static final int TMP = 2;
    private int stepCount;
    private final int num;
    private final String[] towerStr;
    private final Stack<Integer>[] towerStack;

    public HanoiTower(int n, String src, String tgt, String tmp){
        stepCount = 0;
        num = n;
        towerStr = new String[]{src, tgt, tmp};
        towerStack = new Stack[]{new Stack<Integer>(), new Stack<Integer>(), new Stack<Integer>()};
        for(int i = n; i > 0; i--){
            towerStack[SRC].push(i);
        }
    }

    private void move(int n, int src, int tgt, int tmp){
        if(n == 1){
            move(src, tgt);
        }else{
            move(n-1, src, tmp, tgt);
            move(src, tgt);
            move(n-1, tmp, tgt, src);
        }
    }
    private void move(int src, int tgt){
        stepCount++;
        towerStack[tgt].push(towerStack[src].pop());
        System.out.println("=====第"+ stepCount +"步=====");
        System.out.println(towerStr[src]+" -> "+towerStr[tgt]);
        printStack();
    }

    public void move(){
        if(num <= 0) {
            System.out.println("没东西你移个🥚");
            return;
        }
        stepCount = 0;
        move(num, SRC, TGT, TMP);
        System.out.println("共移动"+ stepCount +"步");
    }

    public void printStack(){
        for (int i = 0; i < 3; i++){
            System.out.println(towerStr[i]+": "+towerStack[i]);
        }
    }


    public static void main(String[] args){
        HanoiTower hanoiTower = new HanoiTower(2, "A", "B", "C");
        hanoiTower.printStack();
        hanoiTower.move();
    }
}

栈实现

import java.util.Stack;

public class HanoiTowerWithStack {
    public static final int SRC = 0;
    public static final int TGT = 1;
    public static final int TMP = 2;
    private int stepCount;
    private final int num;
    private final String[] towerStr;
    private final Stack<Integer>[] towerStack;

    private static class Task{
        int n;
        int src;
        int tgt;
        int tmp;
        Task(int n, int src, int tgt, int tmp){
            this.n = n;
            this.src = src;
            this.tgt = tgt;
            this.tmp = tmp;
        }
    }

    public HanoiTowerWithStack(int n, String src, String tgt, String tmp){
        stepCount = 0;
        num = n;
        towerStr = new String[]{src, tgt, tmp};
        towerStack = new Stack[]{new Stack<Integer>(), new Stack<Integer>(), new Stack<Integer>()};
        for(int i = n; i > 0; i--){
            towerStack[SRC].push(i);
        }
    }
    private void move(int src, int tgt){
        stepCount++;
        towerStack[tgt].push(towerStack[src].pop());
        System.out.println("=====第"+ stepCount +"步=====");
        System.out.println(towerStr[src]+" -> "+towerStr[tgt]);
        printStack();
    }

    public void move(){
        if(num <= 0) {
            System.out.println("没东西你移个🥚");
            return;
        }
        stepCount = 0;
        Stack<Task> taskStack = new Stack<>();
        taskStack.push(new Task(num, SRC, TGT, TMP));
        while(!taskStack.isEmpty()){
            Task task = taskStack.pop();
            if(task.n == 1){
                move(task.src, task.tgt);
            }else{
                taskStack.push(new Task(task.n-1,task.tmp,task.tgt,task.src));
                taskStack.push(new Task(1,task.src,task.tgt,task.tmp));
                taskStack.push(new Task(task.n-1,task.src,task.tmp,task.tgt));
            }
        }
        System.out.println("共移动"+ stepCount +"步");
    }

    public void printStack(){
        for (int i = 0; i < 3; i++){
            System.out.println(towerStr[i]+": "+towerStack[i]);
        }
    }


    public static void main(String[] args){
        HanoiTowerWithStack hanoiTower = new HanoiTowerWithStack(2, "A", "B", "C");
        hanoiTower.printStack();
        hanoiTower.move();
    }
}


评论