Java programming to solve the Towers of Hanoi problem with a stack of code examples (non-recursive) _java - JAVA

Source: Hi learning network sensitive and eager Forum www.piaodoo.com welcome to learn from each other

【topic】

  More classic Towers of Hanoi problem, modify the rules a little game here: now restricted from moving directly from the leftmost column to the far right, do not move from the far right to the far left directly, but must go through the middle. When seeking tower N layer when the printing movement and most optimum total number of steps moved.

【answer】

  Previous using a recursive solution to this problem, we are here with a stack of three towers to simulate the Tower of Hanoi, which is not recursive method

  The principle is this: the Towers of Hanoi modified tower can not make any direct moves from left to right, we can not move directly from right to left, but to go through the middle, that is, in fact, can do action, only four: left -> in the -> left, -> right, right -> the

  Stack to simulate the movement of the Tower of Hanoi, in fact, certain elements of a stack pop stack, pushed to another stack, as another stack of stack, understanding this like that, and for that matter, there are two in principle:

  A: small pressure principle, that is, to be pressed into the element values ​​can not be greater than the value of the element to be pressed into the top of the stack of the stack, which is the basic rules of the Tower of Hanoi

  II: adjacent to irreversible principle, that is, my last step of the operation if it is left ->, then the next step is definitely not in operation -> left, or the equivalent of moving over and back again

  With these two principles, we can deduce two very useful conclusions:

  1, an action game must be L-> M

  2, at any time out of the minimum number of steps in the process, only one of four action action does not violate the adjacent small pressure Japan irreversible principle, the other three will be in violation of certain actions

【Code】

the java.util.Stack Import; 
class Demo { 
  public enum {the Action 
    No, LTOM, MToL, mTOR, RToM 
  } 

  // NUM is the number of dishes, left, mid, right represent the left-right three columns 
  public static int hanoi ( NUM int, String left, MID String, String right) { 
    // lS, mS, representative of left, right three of rS stack (analog column) 
    stack <Integer> = lS new new stack <Integer> (); 
    stack <Integer> mS Stack new new = <Integer> (); 
    Stack <Integer> = new new Stack of rS <Integer> (); 
    lS.push (Integer.MAX_VALUE); 
    mS.push (Integer.MAX_VALUE); 
    rS.push (Integer.MAX_VALUE); 
    for (int I = NUM; I> 0; I -) { 
      lS.push (I); 
    } 
    the Action [] = {Record Action.No};  
    int STEP = 0;
    the while (! rS.size () = NUM +. 1) {
      fStackToStack + = STEP (Record, Action.MToL, Action.LToM, lS, mS, left, MID); 
      STEP + = fStackToStack (Record, Action.LToM, Action.MToL, mS, lS, MID, left); 
      STEP + fStackToStack = (Record, Action.MToR, Action.RToM, of rS, mS, right, MID); 
      STEP + = fStackToStack (Record, Action.RToM, Action.MToR, mS, of rS, MID, right); 
    } 
    return STEP; 
  } 

  // preNoAct with the action now to be performed by the reverse operation, nowAct is an operation now to be performed 
  public static int fStackToStack (action [] record, action preNoAct, action nowAct, Stack <Integer> fStack, Stack <Integer> tStack, from String, String to) { 
    IF (Record [0]! = preNoAct fStack.peek && () <tStack.peek ()) {  
      tStack.push (fstack. POP ());
      System.out.println ( "the Move" + tStack.peek () + "" + from + "->" + to) ;
      record[0] = nowAct;
      return 1;
    }
    return 0;
  }

  public static void main(String[] args){
    int i = hanoi(3,"left","mid","right");
    System.out.println("一共走了" + i + "步");
  }
}

 

to sum up

This article is more about Java programming with the entire contents of a stack to solving the code examples (non-recursive) Tower of Hanoi problem, we want to help. Interested friends can see: Java Monte Carlo algorithms for pi approximation of detailed examples, out of the maze of genetic algorithm Java, Java achieve four mixed computing code samples, and, what problems can leave a message at any time, we welcome the discussions and exchange.

The original address is: http: //www.piaodoo.com/thread-13253-1-1.html stockings control www.txdah.com 131 outside www.buzc.org enjoyable learning can help to learn better!

Guess you like

Origin www.cnblogs.com/txdah/p/12093775.html