佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1536|回复: 4

求programming 高手!关于Tower of hanoi

[复制链接]
发表于 20-8-2011 09:58 PM | 显示全部楼层 |阅读模式
本帖最后由 辣椒_小咖喱 于 23-11-2011 01:04 PM 编辑

我这里有tower of hanoi 的sample,但是我需要用linked stack 来完成

Tower.java
----------------------------------------------------------------------------------------------------
  1. import java.util.Scanner;

  2. public class Tower{

  3. int count=0;

  4. public void solveTower(int disks, int source, int destination, int temp){
  5. ++count;
  6. if(disks==1){
  7. System.out.printf("\n%d --> %d", source, destination);

  8. return;

  9. }

  10. solveTower(disks-1, source, temp, destination);

  11. System.out.printf("\n%d --> %d", source, destination);

  12. solveTower(disks-1, temp, destination, source);

  13. }

  14. public static void main(String[]args){
  15. System.out.println("Enter disks");

  16. int disks = new Scanner(System.in).nextInt();

  17. Tower t = new Tower();

  18. t.solveTower(disks, 1,3,2);

  19. System.out.println("\n"+t.count);
  20. }


  21. }
复制代码

------------------------------------------------------------------------------------------------------

这是linked stack 的interface

  1. public interface Stack<E> {
  2.   /** Reinitialize the stack. */
  3.   public void clear();

  4.   /** Push an element onto the top of the stack.
  5.       @param it Element being pushed onto the stack.*/
  6.   public void push(E it);

  7.   /** Remove and return top element.
  8.       @return The element at the top of the stack.*/
  9.   public E pop();

  10.   /** @return A copy of the top element. */
  11.   public E topValue();

  12.   /** @return Number of elements in the stack. */
  13.   public int length();
  14. };

复制代码

-------------------------------------------------------------------------------
  1. class LStack<E> implements Stack<E> {
  2.   private Link<E> top;
  3.   private int size;

  4.   class Link<E> {
  5.   private E element;
  6.   private Link<E> next;

  7.   // Constructors
  8.   Link(E it, Link<E> nextval)
  9.     { element = it;  next = nextval; }
  10.   Link(Link<E> nextval) { next = nextval; }

  11.   Link<E> next() { return next; }
  12.   Link<E> setNext(Link<E> nextval)
  13.     { return next = nextval; }
  14.   E element() { return element; }
  15.   E setElement(E it) { return element = it; }
  16.   }



  17. public LStack() { top = null; size = 0; }

  18. public LStack(int size) { top = null; size = 0;}



  19. public int length() { return size; }

  20. public void clear() { top = null; size = 0; }


  21. /** @return Top value */

  22. public E topValue() {

  23. assert top != null : "Stack is empty";

  24. return top.element();

  25. }


  26. /** Put "it" on stack */

  27. public void push(E it) {

  28. top = new Link<E>(it, top);

  29. size++;

  30. }


  31. /** Remove "it" from stack */

  32. public E pop() {

  33. assert top != null : "Stack is empty";

  34. E it = top.element();

  35. top = top.next();

  36. size--;

  37. return it;

  38. }

  39. public static void main(String[] args)
  40.      {

  41.       }
  42. }
复制代码

-------------------------------------------------------------------------------------
我要怎样把Tower.java用linked stack的方法来完成?
回复

使用道具 举报


ADVERTISEMENT

发表于 23-8-2011 03:25 PM | 显示全部楼层
我觉得概念上还是一样(如果你的solveTower是对的话)。。。
有点久没写java了,可能有点syntax error。。。

  1. import java.util.Scanner;

  2. public class Tower{
  3. int count=0;
  4. public void solveTower(int disks, LStack<int> source, LStack<int> destination, LStack<int> temp){
  5. ++count;
  6. if(disks==1){
  7. System.out.printf("\n%d --> %d", source, destination);
  8. destination.push(source.pop());
  9. return;
  10. }
  11. solveTower(disks-1, source, temp, destination);

  12. System.out.printf("\n%d --> %d", source, destination);
  13. destination.push(source.pop());

  14. solveTower(disks-1, temp, destination, source);

  15. }

  16. public static void main(String[]args){
  17. System.out.println("Enter disks");

  18. int disks = new Scanner(System.in).nextInt();

  19. Tower t = new Tower();
  20. LStack<int> src = new LStack<int>();
  21. LStack<int> dest = new LStack<int>();
  22. LStack<int> tmp = new LStack<int>();

  23. for(int i=1;i<=disks;i++){
  24. src.push(i);
  25. }

  26. t.solveTower(disks, src,dest,tmp);

  27. System.out.println("\n"+t.count);
  28. }
  29. }
复制代码
回复

使用道具 举报

 楼主| 发表于 3-9-2011 09:48 PM | 显示全部楼层
本帖最后由 辣椒_小咖喱 于 3-9-2011 09:51 PM 编辑

你的用不到><
这是我另外做的以下。。。。
  1. import java.util.Scanner;

  2. public class tower{
  3.         private LStack<Integer> PoleA = new LStack<Integer>();
  4.         private LStack<Integer> PoleB = new LStack<Integer>();
  5.         private LStack<Integer> PoleC = new LStack<Integer>();
  6.         private int disk = 0;
  7.         private int numberOfMoves = 0;

  8.         public tower(){
  9.         }

  10.         public tower(int disk){
  11.                 this.disk=disk;
  12.                 init();
  13.         }

  14.         private void init(){
  15.                 for(int i=disk;i>=1;i--)
  16.                         PoleA.push(i);        //i is the size of the disc
  17.         }

  18.         private void move(LStack<Integer> from, LStack<Integer> to){
  19.                 to.push(from.pop());
  20.                 //System.out.printf("\n%d --> %d", to.topValue(), from.topValue());
  21.                 numberOfMoves++;
  22.         }

  23.         private void legalMove(LStack<Integer> PoleA, LStack<Integer> PoleB){
  24.                 if(PoleC.length()==disk)
  25.                         return; //skip if already completed toh
  26.                 if(PoleB.length() == 0)
  27.                         move(PoleA,PoleB);
  28.                 else if(PoleA.length()==0 || PoleA.topValue() > PoleB.topValue())
  29.                         move(PoleB,PoleA);
  30.                 else
  31.                         move(PoleA,PoleB);
  32.         }

  33.         private void solve(){
  34.                 while(PoleC.length()!=disk)
  35.                         if(disk%2==0) {        //even number
  36.                                 legalMove(PoleA,PoleB);
  37.                                 legalMove(PoleA,PoleC);
  38.                                 legalMove(PoleB,PoleC);
  39.                         } else {
  40.                                 legalMove(PoleA,PoleC);
  41.                                 legalMove(PoleA,PoleB);
  42.                                 legalMove(PoleB,PoleC);
  43.                         }
  44.         }
  45.         
  46.         public void solveTower(int disk, int source, int destination, int temp){
  47.         
  48.                 if(disk==1){
  49.                         //destination.push(source.pop());
  50.                         System.out.printf("\n%d --> %d", source, destination);
  51.                         return;
  52.                 }
  53.                         
  54.         
  55.                 solveTower(disk-1, source, temp, destination);
  56.                 //System.out.println("\ndone 1");
  57.                 System.out.printf("\n%d --> %d", source, destination);
  58.                 //System.out.println("\ndone 2");
  59.                
  60.                 solveTower(disk-1, temp, destination, source);
  61.                 //System.out.println("\ndone 3");
  62.         }

  63.         public static void main(String[] args){
  64.                 tower game = new tower();
  65.                 do{
  66.                         try{
  67.                                 System.out.print("Enter disks:");
  68.                                 game = new tower(new Scanner(System.in).nextInt());
  69.                                 if(game.disk<1)
  70.                                         throw new Exception();
  71.                                 break;
  72.                         }catch(Exception e){
  73.                                 System.out.println("Invalid value entered! Try again!");
  74.                         }
  75.                 }while(true);
  76.                 game.solveTower(game.disk,1,2,3);
  77.                 long t1 = System.nanoTime();
  78.                 game.solve();
  79.                 long t2 = System.nanoTime();
  80.                 System.out.println("\nTotal number of moves:\t"+game.numberOfMoves);
  81.                 System.out.println("Execution time:\t"+((t2-t1)*1e-6)+"ms");
  82.         }
  83. }
复制代码
回复

使用道具 举报

发表于 6-11-2011 01:00 PM | 显示全部楼层
可以参考这个使用 JavaScript 设计的 Tower of Hanoi 问题。
http://fyhao.com/lab/math/hanoi/
回复

使用道具 举报

发表于 6-11-2011 11:36 PM | 显示全部楼层
本帖最后由 algorithm 于 6-11-2011 11:38 PM 编辑

以下是我根据你的Tower.java用java.util.stack改成的TowerStack.java。见笑见笑
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.Stack;

  4. public class TowerStack
  5. {
  6.     static int movecount = 0;
  7.     static Stack stackA = new Stack();
  8.     static Stack stackB = new Stack();
  9.     static Stack stackC = new Stack();
  10.    
  11.     public static void solve(int nDiscs, Stack source, Stack dest, Stack temp){
  12.         if(nDiscs <= 0){
  13.             return;
  14.         }
  15.             solve(nDiscs-1, source, temp, dest);
  16.             dest.push(source.pop());
  17.             movecount++;
  18.             printStacks();
  19.             solve(nDiscs-1, temp, dest, source);
  20.     }
  21.    
  22.     public static void printStacks(){
  23.         System.out.println();
  24.         printStack(stackA);
  25.         System.out.print(" , ");
  26.         printStack(stackB);
  27.         System.out.print(" , ");
  28.         printStack(stackC);
  29.     }
  30.    
  31.     public static void printStack(Stack s)
  32.     {
  33.         System.out.print(s.toString());
  34.     }
  35.    
  36.     public static void main(String[] args)
  37.     {
  38.         try
  39.         {
  40.             while (true)
  41.             {
  42.                 movecount = 0;
  43.                
  44.                 System.out.print("\nEnter the number of discs (-1 to exit): ");

  45.                 int discCount = 0;
  46.                 String inputString = "";

  47.                 InputStreamReader input = new InputStreamReader(System.in);
  48.                 BufferedReader reader = new BufferedReader(input);
  49.                 inputString = reader.readLine();
  50.                 discCount = Integer.parseInt(inputString);
  51.                
  52.                 if (discCount == -1)
  53.                 {
  54.                     System.out.println("System Exit!");
  55.                     System.exit(0);
  56.                     return;
  57.                 }
  58.                
  59.                 for (int i = discCount; i >= 1; i--)
  60.                     stackA.push(i);
  61.                
  62.                 printStacks();
  63.                
  64.                 solve(discCount, stackA, stackC, stackB);
  65.                
  66.                 System.out.println("Total Moves = " + movecount);
  67.                
  68.                 stackC.clear();
  69.             }
  70.         }
  71.         catch (Exception e)
  72.         {
  73.             e.printStackTrace();
  74.             System.exit(0);
  75.         }
  76.     }
  77. }
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 15-6-2024 07:43 PM , Processed in 0.058583 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表