Recursive backtracking to solve 8 Queens

Today, learning the classic 8-queens problem, and strengthen the understanding of the recursive call, always put yourself before you write a recursive export conditions it wrong, cause logic errors, often throwing error stack overflow.

In fact, it simply recursive call, before calling, in order to understand certain recursive exports where, at the time of the recursive call, how to make recursive code ever closer toward the exit,

Eventually find the exit recursive. This issue want to see, recursive call it half the success.

Before calling a recursive also found that, in the final analysis is calculated by recursion stack, if you call something in the pile of recursion, the new must not be out of the object.

And when the recursive call to try to call the recursive use of outside resources into reducing resource recursive call.

Here is a simple example, the recursive method to write the output of a repeat statement, the code itself is no problem, but if the number of calls the method too, will still throw an error stack overflow.

Code tested with a simple output statement, on my computer, using a recursive call to String methods directly, once more than 6200 times may throw an exception,

StringBuilder call with recursive method, once more than 12,000 may throw an exception

Today the teacher's method of recursive 8 Queen of really classic. Resource-not many, but the teacher said, in fact, this algorithm probably have multiple recursive method calls 14000

Thus, in fact, the recursive method code resources used and the complexity of the code itself does not matter much, the key is to see how the code design,

How can also promise not to take up too much computer resources when calling.

These are some superficial understanding of myself for recursion.

Here I put rewritten 8 queens problem solving recursive code.

. 1  Package com.recursion;
 2  
. 3  / * 
. 4  * 8 queens problem solving recursive backtracking
 5  * demand, according to the rules of chess, 8 queens are placed on the board, each queen can not eat next move sub
 6  * is placed on the board when the 8 queens, Queen position 8 as a positive solution, calculated total number of positive Solutions
 7  * 
 8  * recursive backtracking idea
 9  * the first queens on a first row, first column, second discharge Queen to see if the child can eat next move,
 10  * can not eat the child is placed third queen, placed under a midnight, back position possible, change
 11  * start solving back into the eighth when the Queen
 12  * get a the correct solution would be to return a stack, the stack would in all likelihood have to try it again,
 13  * return on a stack solved again, the way back to the first Queen
 14  * 
 15   * / 
16  public  class EightQueen {
 17      // 1, a total of eight queens 
18     int max =. 8 ;
 . 19      // . 1, the array definition storage solution 
20 is      int [] Array = new new  int [max];
 21 is      // . 5, the definition of variables solution 
22 is      static  int COUNT = 0 ;
 23 is      public  static  void main (String [] args) {
 24          // the TODO Auto-Generated method Stub
 25          // . 1, represented by one-dimensional array board, the index represents the row coordinate, represents the column coordinate value
 26          // define a queen put the position of the output process
 27          / / 4, a test method called recursively 
28          EightQueen queen8 = new new EightQueen ();
 29         queen8.check (0 );
 30          // . 5, a total of how many statistics solution 
31 is          System.out.printf ( "% d a total seed solution" , COUNT);
 32      }
 33 is      
34 is      // . 3, Queen method defined place, each check will enter a recursive loop, resulting in a positive know Solutions 
35      Private  void check ( int n-) {
 36          // n-is 8, the positive solution method, recursive outlet 
37 [          IF (n-== max) {
 38 is              Print ();
 39              return ;
 40          }
 41 is          // placed Queen, determines whether a collision 
42 is          for ( int I = 0; I <max; I ++ ) {
43              // put into the first row n Queen 
44 is              Array [n] = I;
 45              IF (Judge (n)) {
 46 is                  // If not conflict, began to put the n + 1 queens, recursion starts 
47                  Check (n + 1 );
 48              }
 49              // If a conflict, a recursive loop continues to the next line 
50          }
 51      }
 52 is      
53 is      // 2, see the definition of the n-th place Queen method satisfies placement rules 
54      / ** 
55       * 
 56       * @param n denotes the n-queens
 57 is       * @return 
58       * / 
59      Private  BooleanJudge ( int n) {
 60          for ( int I = 0; I <n; I ++ ) {
 61 is              // judges whether the n th and the n-1 queens queens are in the same column, n increment operation itself, does not require Analyzing row
 62              // determines the n-th and i-th Queen Queen are in the same oblique line 
63 is              iF (Array [i] == Array [n] || the Math.abs (n - i) the Math.abs == (Array [n-] - Array [I])) {
 64                  return  to false ;
 65              } 
 66          }
 67          return  to true ;
 68      }
 69      
70      // . 1, a method defined printout 
71 is      Private  void Print () {
 72         count++;
73         for (int i = 0; i < array.length; i++) {
74             System.out.print(array[i] + " ");
75         }
76         System.out.println();
77     }
78 
79 }

Code is not much, but to solve a complex classical computational problems.

Guess you like

Origin www.cnblogs.com/zzzzzpaul/p/11588020.html