河内塔 - Java

时间:2014-11-12 08:33:56

标签: java towers-of-hanoi

我在java中有这个河内塔计划,我能够让它工作,但我不能为我的生活弄清楚如何让它展示它是如何做到的。例如,"磁盘1从TowerA到TowerC","磁盘2从TowerA到TowerB"等等。

这是我的代码:

有没有办法将" Disk#From(TowerA,TowerB或TowerC)添加到(TowerA,TowerB或TowerC)"?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:1)

试试这个

public class TowerApp
{
    static int nDisks = 3;
    static public LinkStack A = new LinkStack("A");
    static public LinkStack B = new LinkStack("B");
    static public LinkStack C = new LinkStack("C");


    static public void doTowers(int nDisks, LinkStack source, LinkStack temp, LinkStack dest)
    {
        if(nDisks <= 4)
            if ((nDisks % 2) == 0)
            {   
                displayStacks(source, temp, dest);
                nDisks = nDisks - 1;
                long dn = source.pop();
                temp.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
                displayStacks(dest, source, temp);

                dn = source.pop();
                dest.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());

                doTowers(nDisks, temp, source, dest);
            } 
            else
            {
                displayStacks(source, dest, temp);
                nDisks = nDisks - 1;
                long dn =  source.pop();
                dest.push(dn);
                System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
                displayStacks(temp, source, dest);
            }

        /*          else if (nDisks >= 5)
        {
            doTowers(nDisks - 2, source, temp, dest);
            temp.push(source.pop());
            doTowers(nDisks - 2, dest, source, temp);
            dest.push(source.pop());
            doTowers(nDisks - 1, temp, source, dest);
        }*/
    }
    static public void displayStacks(LinkStack source, LinkStack temp, LinkStack dest)
    {           
        long n = source.pop();
        temp.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
        PrintStacks();
        n = source.pop();
        dest.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
        PrintStacks();
        n= temp.pop();
        dest.push(n);
        System.out.println("Disk #"+n+" moved from Tower "+temp.getName() +" to Tower "+dest.getName());
        PrintStacks();
    }

    static public void PrintStacks()
    {
        A.displayStack("TowerA");
        B.displayStack("TowerB");
        C.displayStack("TowerC");
        System.out.println("");
    }

    public static void main(String[] args)
    {
        for (int i = nDisks; i >= 1; i--)
        {
            A.push(i);
        }
        PrintStacks();

        doTowers(nDisks, A, B, C);
    }
}

和LinkStack类看起来像

class LinkStack
{

    private LinkedList theList;
    private String name;

    public LinkStack(String name)             // constructor
    {
        theList = new LinkedList();
        this.name =  name;
    }
    public void push(long j)     // put item on top of stack
    {
        theList.addFirst(j);
    }
    public long pop()            // take item from top of stack
    {
        return (Long)theList.removeFirst();
    }
    public boolean isEmpty()       // true if stack is empty
    {
        return ( theList.isEmpty() );
    }
    public void displayStack(String name)
    {
        System.out.print(name + ": (top-->bottom): ");
        System.out.println(theList);
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

输出:

TowerA: (top-->bottom): [1, 2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): []

Disk #1 moved from Tower A to Tower C
TowerA: (top-->bottom): [2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): [1]

答案 1 :(得分:0)

要获取堆栈的名称,请在LinkStack类中添加一个getter方法:

public String getName() {
  return this.name;
}

要创建所需的输出,请在名为System.out.println(...)的方法中调用doTowers()。要获取当前堆栈的名称,您可以调用source.getName()temp.getName()dest.getName()

此外,向peek()类添加LinkStack方法非常有用,它会返回内部列表的第一个元素,不用删除它。或者,您可以将source.pop()的返回值存储在局部变量中,这样您就可以输出它并在之后将其传递给dest.push()