Iterative and recursive implementation of the Tower of Hanoi problem

  1  /** 
  2  * @author  http://www.cnblogs.com/Xuxy1996/p/8933056.html 
  3  * @author xuxy
   4  *
   5   */ 
  6  public  class second {
   7  
  8      /** 
  9       * implement the dish move from x to y
 10       * 
 11       * @param n
 12       * n plate
 13       * @param x
 14       * start position
 15       * @param y
 16       * end position
17       */ 
18      private  static  void move( int n, int x, int y) {
 19          System.out.println(n + ":" + ( char ) (x + 65) + "---->" + ( char ) (y + 65 ));
 20      }
 21  
22      /** 
23       * The core of understanding the recursive implementation of Tower of Hanoi is to start from the end position of the bottom plate, for example:
 24       * If there are 4 plates, ABC3 Pillar, in order to move plate 4 to column C, then plate 123 needs to be moved to column B to ensure the smooth movement of plate 4;
 25       * Now the scale of the problem is reduced by one, how to move plate 123 to column B, the idea is the same as above, In order to move plate 3 to column B, plate 12 needs to be moved to column C at this time,
 26       * Finally, in order to move plate 2 to column C, plate 1 needs to be moved to column B;
 27      * In general, when plate n needs X column, the remaining plate n-1 should move from the starting column to the Y column.
 28       * 
 29       * @param num
 30       * current plate number
 31       * @param ABC
 32       * The position of the pillar
 33       */ 
34      public  static  void hanoi_1( int num, int A, int B, int C) {
 35          if (num == 1 )
 36              move(num, A, C);
 37          else {
 38              hanoi_1(num - 1, A, C, B);
 39              move(num, A, C);
 40              hanoi_1(num - 1 , B, A, C);
 41          }
 42      }
 43  
44      /** 
45       * Iteratively implement the Tower of Hanoi problem In essence, recursion is also used, but this recursive stack is operated by itself, not the system
 46       * Similarly, referring to the recursive algorithm above, for the largest dish, what we need to do is to process the next level of small dishes first. 47 * One thing  to
      note is that the stack is first-in-last-out, so compared to the recursive Tower of Hanoi, the order of processing the dishes here needs to be reversed, for example: Premise: 2 dishes, 3 pillars ABC,
 48       * In the above recursive algorithm, in order to move the No. 2 plate to the C column, it is necessary to move the No. 1 plate to the B column, and after the No. 2 plate moves to the C column, the No. 1 plate moves to the C column
 49       * In order to meet the actual requirements The order above, so the stacking order should be reversed: 1 B->C
 50       * 2 A->C
 51      * 1 A->B;
 52       * 
 53       * @param num
 54       * Number of plates to be processed
 55       * @param ABC
 56       * Post position
 57       */ 
58      public  static  void hanoi_2( int num, int A, int B, int C) {
 59          Stack<data> stack = new Stack<data> ();
 60          stack.push( new data(num, A, B, C, num));
 61         while (!stack.isEmpty()) {
 62             data d = stack.pop();
 63             A = d.x;
 64             B = d.y;
 65             C = d.z;
 66             num = d.num;
 67             if (num == 1)
 68                 move(d.number, A, C);
 69             else {
 70                 stack.push(new data(num - 1, B, A, C, num - 1));
 71                 stack.push(new data(1, A, B, C, num));
72                  stack.push( new data(num - 1, A, C, B, num - 1 ));
 73              }
 74          }
 75      }
 76  
77      /** 
78       * The data structure that holds the current state of the dish on the stack
 79       * 
 80       * @author xuxy
 81       *
 82       */ 
83      private  static  class data {
 84          public data( int n, int a, int b, int c, int m) {
85              num = n; // Number of remaining plates 
86              x = a; // Start position 
87              y = b; // Middle position 
88              z = c; // End position 
89              number = m; // Current plate number 
90          }
 91  
92          int num, x, y, z, number;
 93      }
 94  
95      /** 
96       * static method for test data
 97       * 
 98       * @param args
 99       */ 
100      public static void main(String[] args) {
101         Scanner sc = new Scanner(System.in);
102         int num = sc.nextInt();
103         hanoi_1(num, 0, 1, 2);
104         System.out.println("------------------------------");
105         hanoi_2(num, 0, 1, 2);
106     }
107 
108 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324783067&siteId=291194637