Java汉诺塔的问题

Python017

Java汉诺塔的问题,第1张

import java.util.LinkedList

public class Hanoi {

    private int num

    private Peg[] pegs = new Peg[3]

    public Hanoi(int num) {

        this.num = num

        pegs[0] = new Peg("A")

        pegs[1] = new Peg("B")

        pegs[2] = new Peg("C")

        for (int i = num i > 0 i--) {

            pegs[0].pushDown(new Plate(i))

        }

    }

    public void start() {

        multiMove(pegs[0], pegs[2], pegs[1], num)

    }

    /**

     * Move a given number plates from one peg to another

     * @param from from this peg

     * @param to to this peg

     * @param temp the temp peg to use

     * @param count how many plates to move

     */

    public void multiMove(Peg from, Peg to, Peg temp, int count) {

        if (count == 1) {

            move(from, to)

            return

        }

        multiMove(from, temp, to, count - 1)

        move(from, to)

        multiMove(temp, to, from, count - 1)

    }

    public void move(Peg from, Peg to) {

        Plate p = from.popUp()

        to.pushDown(p)

        System.out.print("Moving " + p.size + " from " + from.name + " to " + to.name + "      ")

        printPegs()

    }

    public void printPegs() {

        System.out.println(pegs[0].name + ": " + pegs[0] +

                "    " + pegs[1].name + ": " + pegs[1] +

                "    " + pegs[2].name + ": " + pegs[2])

    }

    public static class Plate {

        int size

        Plate(int size) {

            this.size = size

        }

        @Override

        public String toString() {

            return String.valueOf(size)

        }

    }

    public static class Peg {

        LinkedList<Plate> plateList = new LinkedList<>()

        String name

        public Peg(String name) {

            this.name = name

        }

        public void pushDown(Plate p) {

            plateList.push(p)

        }

        public Plate popUp() {

            return plateList.pop()

        }

        @Override

        public String toString() {

            return plateList.toString()

        }

    }

    public static void main(String[] args) {

        Hanoi game = new Hanoi(3)

        game.printPegs()

        game.start()

    }

}

这样应该可以了

如果还有那个地方不懂的,建议你研究下汉诺塔算法

import

java.io.BufferedReader//引入IO包中的BufferedReader

import

java.io.IOException//引入IO包中的IO异常处理

import

java.io.InputStreamReader//引入IO包中的InputStreaReader

public

class

Hinoi

{

//主类

static

int

m=0//定义移动的次数

//主程序入口--main方法

public

static

void

main(String[]

args)

{

//创建BufferedReader对象,InputStream输入流

BufferedReader

bf

=

new

BufferedReader(new

InputStreamReader(System.in))

System.out.println("请输入盘子的个数:")

try

{

int

sl

=

Integer.parseInt(bf.readLine().toString())//接收总盘子个数

toMove(sl,"A","B","C")//调用移动方法

A-->C

}

catch

(NumberFormatException

e)

{捕获NumberFormatException异常

//

TODO

Auto-generated

catch

block

e.printStackTrace()//打印异常

}

catch

(IOException

e)

{//捕获IOException异常

//

TODO

Auto-generated

catch

block

e.printStackTrace()//打印异常

}

System.out.println("总共移动了:"+m+"

次数")//打印移动次数

}

//移动方法

private

static

void

toMove(int

sl,

String

one,

String

two,String

three)

{

if(sl==1){//如果只有一个盘子,则直接移动到C柱

System.out.println("盘子"+sl+"

"+one+"---->"+three)

}else{//如果总盘数大于1,则递归调用移动方法

//把所有的数量为sl-1的盘子全部从A移到到B(C作为一个过渡),好提供一个最下面的位置给最大盘子到C

toMove(sl-1,one,three,two)

System.out.println("盘子"+sl+"

"+one+"---->"+three)

//把所有的剩余的盘子从B移动到C(A作为一个过渡)

toMove(sl-1,two,one,three)

}

m++

}

}