Recursive realization of n (Classic eight queens Problem) Queen problem, recursive queen
Problem description: the eight queens question is a question with the background of chess: how can we place eight queens on the 8x8 chess board, so that no queen can directly eat any other queen? To achieve this goal, neither queen can be in the same horizontal, vertical, or diagonal line. This problem can be promoted to the problem of Queen n.
Solution: the matrix of n * n is used to recursion each point. When the number of Queens reaches n, the system determines that if the question conditions are met, the answer is plus one (number ++ ), otherwise, the traversal continues.
Method for saving the Queen's points: Construct a two-dimensional array reserve [] []. When reserve [I] [j] = 1, there will be a queen at this point, if reserve [I] [j] = 0, the queen can exist at this point and set it to one.
Determine the number of Queens, define an int sign, recursively traverse when sign <8, and repeat the previous operation; otherwise, judge the reserve array, determine the coordinates of the points equal to 1 in the array, whether the meaning of the question is satisfied, after the judgment, the current point is set to 0.
To judge the x and Y axes, you only need to determine whether there are equal coordinate values.
Determines whether the absolute values of the Offset coordinate values between each two points are equal. (recursive traversal of each point is required here.) If the values are equal, the point is repeated on the diagonal line. false is returned, if they are not equal, the dot is not repeated on the slash, and true is returned.
First define global variables:
Private static int number = 0; // number of answers int count = 0; // The following array subscript static String [] str; // Save the String array with the correct answer, to remove duplicates
Define the main function:
Public static void main (String [] args) {com c = new com (); System. out. print ("Enter the Queen's number n:"); s = new second (System. in); int n = Integer. parseInt (s. nextLine (); int [] [] reserve = new int [n] [n]; // stores the Queen's status str = new String [n * 100]; int sign = 1; c. startRun (reserve, n, sign); System. out. println (number );}
The following is the traversal function:
Public void startRun (int [] [] reserve, int n, int sign) {for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (reserve [I] [j] = 0) reserve [I] [j] = 1; // This point is a queen else {continue;} if (sign = n) {if (checkAllQuean (reserve, n )) {// determine the position of n Queen output (reserve, n); // an output function that outputs the point System of n queen. out. println (); number ++ ;}} else if (sign <n) {startRun (reserve, n, sign + 1 ); // perform the traversal operation} reserve [I] [j] = 0 ;}}}
The following is an array reserve for determining the Queen's position:
/*
* Check whether two queens are in the same row, in the same column, or on the same diagonal line.
* If yes, false is returned.
* Returns true if no data exists.
*/
Public boolean checkAllQuean (int [] [] reserve, int n) {int [] x = new int [n]; int x1 = 0; int [] y = new int [n]; int y1 = 0; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {if (reserve [I] [j] = 1) {x [x1 ++] = I; y [y1 ++] = j ;}}// obtain the coordinate of all the queens (x1 = 0; x1 <n; x1 ++) {for (y1 = 0; y1 <n; y1 ++) {if (x1 = y1) continue; if (! CheckTwoQuean (x [x1], y [x1], x [y1], y [y1]) {// compare the dot coordinate of every n queens return false ;}} if (! CheckReNumber (x, y, n) {return false;} return true ;}
Delete the function that repeats the answer:
/** Save the correct answer array in a String, used to avoid repeated * If repeated * if false * is returned, true */public boolean checkReNumber (int [] x, int [] y, int n) {String test = null is returned; for (int j = 0; j <n; j ++) {test + = x [j] + "" + y [j] + "";} for (String st: str) {if (st = null) continue; if (st. equals (test) {return false ;}str [count ++] = test; return true ;}
The following judges the positions of the two queens:
/** Check whether two queens are in the same row, in the same column, or * exist on the same diagonal line, return false * do not exist, return true */public boolean checkTwoQuean (int I, int j, int m, int n) {if (I = m) return false; else if (j = n) return false; else if (Math. abs (m-I) = Math. abs (n-j) return false; else {return true ;}}
Below is the function that outputs the reserve point:
public void output(int[][] reserve , int n){for(int k = 0; k < n;k++){for(int h = 0;h< n;h++){if(reserve[k][h] == 0)continue;System.out.print(k+","+h+" ");}}}
But the efficiency is extremely low.
Output case:
Enter Queen's number n: 40, 1, 0, 2, 0, 1 2
Queen n's problem is resolved when the value is greater than or equal to 4.