Eight Queens problem introduced
Eight queens problem is an old and well-known problems, is a typical case of backtracking algorithms . The problem is an international chess player Max Bethel presented in 1848:
Placed on the international chess 8 × 8 grid of eight Queens , it can not attack each other, namely: any two queens can not be in the same line, same column or the same slash and asked how many pendulum method .
Then we use the program to calculate how many pendulum method, which is the use of backtracking algorithm.
analysis:
1) The first queen to discharge a first column of the first row
2) The second Queen on the first column of the second row, and then determine whether OK [That judgment is a conflict], if not OK, continue to be on the second row, third row, followed by all the columns are done and found a suitable
3) The third continued the Queen, or the first column, the second column ...... until eight queens can not be placed in a position of conflict, be found a correct solution
4) When the time to get a correct solution, when the stack falls back on a stack, will begin to backtrack, the first is about a queen, all right solution into the first row, all obtained.
5) is then put back to continue with a second column Queen, continues to loop back to step 1,2,3,4
Description: In theory should create a two-dimensional array to represent the board, but actually by the algorithm, using a one-dimensional array to solve the problem.
arr [8] = {0, 4, 7, 5, 2, 6, 1, 3} // corresponds arr index indicates the first few lines, i.e., the first of several Queen, arr [i] = val, val represents the i +1 Queen, on the i + val + 1 column 1 of the first row
Implementation code:
Package Penalty for com.recursion;
/ **
. * the Created by wanbf ON 2019/6/8 eight queens problem
* /
public class Queue8 {
// define a max represents the total number of queens
int max = 8 ;
// define an array array, save results Queen placement position, such ARR = {0,. 4,. 7,. 5, 2,. 6,. 1,. 3}
int [] Array = new new int [max];
static int COUNT = 0 ;
static int judgeCount = 0 ;
public static void main (String [] args) {
// test one, if the 8 queens correct
queue8 queue8 = new new queue8 ();
queue8.check ( 0 );
System.out.printf ( "Solution A total of% d" , COUNT);
System.out.printf ( "total number of judgment times conflict% d", judgeCount); // 1.5W
}
/ / prepared by a method of placing the n-queens
// special note: check every time when the recursion proceeds to check in there for (int i = 0; i <max; i ++), so there will be back
Private void check ( int n-) {
iF (n-== max) { // n-=. 8, in fact, since the queen put 8
Print ();
return ;
}
// turn into the queen, and determines whether a collision
for ( int I = 0 ; i <max; i ++) {
// put the current Queen n, into column 1 of the row
Array [n] = i;
// is determined when placed to the n-th column i Queen, whether a collision
IF (Judge (n)) { // do not conflict
// Next, n + 1 queen discharge, i.e., recursion starts
Check (n + 1); //
}
// If a conflict, continuing with the array [n] = i; n-queens soon placed in after the Bank was shifted one position
}
}
// Check if we put the n-th Queen, the Queen went to detect whether and already placed Queen conflict
/ **
*
* @param n n th Queen
* @ return
* /
Private boolean Judge ( int n-) {
judgeCount ++ ;
for ( int I = 0; I <n; I ++ ) {
// Description
// 1. Array [I] == Array [n] denotes the n-th determines whether the queen and the n-1 preceding the Queen in the same column
// 2. the Math.abs (Ni) == the Math.abs (Array [n] - Array [i]) represents the determination whether the n-th and the i Queen Queen are in the same slash
// n. 1 = place the first two. 1 = n-Array. 1 [. 1] =. 1
// the Math.abs (1-0). 1 == the Math.abs (Array [n-] - Array [I]) = the Math.abs (1-0) = 1
// 3 determines whether the same line, it is not necessary, each time incrementing n-
iF (Array [I] == Array [n-] || the Math.abs (Ni) == the Math.abs (Array [n- ] - Array [I])) {
return to false ;
}
}
Return to true ;
}
// write a method that can display the location of the queen output
Private void Print () {
COUNT ++ ;
for ( int I = 0; I <be array.length; I ++ ) {
the System.out. Print (Array [I] + "" );
}
System.out.println ();
}
}
输出
0 4 7 5 2 6 1 3
0 5 7 2 6 3 1 4
0 6 3 5 7 1 4 2
0 6 4 7 1 3 5 2
1 3 5 7 2 0 6 4
1 4 6 0 2 7 5 3
1 4 6 3 0 7 5 2
1 5 0 6 3 7 2 4
1 5 7 2 0 3 6 4
1 6 2 5 7 4 0 3
1 6 4 7 0 3 5 2
1 7 5 0 2 4 6 3
2 0 6 4 7 1 3 5
2 4 1 7 0 6 3 5
2 4 1 7 5 3 6 0
2 4 6 0 3 1 7 5
2 4 7 3 0 6 1 5
2 5 1 4 7 0 6 3
2 5 1 6 0 3 7 4
2 5 1 6 4 0 7 3
2 5 3 0 7 4 6 1
2 5 3 1 7 4 6 0
2 5 7 0 3 6 4 1
2 5 7 0 4 6 1 3
2 5 7 1 3 0 6 4
2 6 1 7 4 0 3 5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