The eight queens issue is an ancient and famous issue and is a typical example of backtracking algorithms. This problem was proposed by the famous mathematician Gauss in the 19th century in 1850: Eight queens were placed on the 8x8 lattice International elephant board, so that they could not attack each other, that is to say, neither of the two queens can be in the same row, column, or diagonal line. [Interview questions for a famous British computer graphics and image Company]
Algorithm 1: A typical return algorithm. Print out the final arrangement of the 8 queens.
Resolution: recursively implements the n queen problem.
Algorithm analysis:
Arrays A, B, and C are used to mark conflicts. array a indicates column conflicts, from a [0] to A [7] represents columns 0th to 7th. If a column already has a queen, it is 1; otherwise, it is 0.
Array B indicates the primary diagonal conflict, which is B [I-j + 7], that is, from B [0] ~ B [14]. If a principal has a queen on the diagonal line, the value is 1; otherwise, the value is 0.
Array C indicates the diagonal line conflict, which is C [I + J], that is, from C [0] ~ C [14]. If there is a queen on the diagonal line, the value is 1; otherwise, the value is 0.
Package org. luyang. csdn;
Public class eightqueue {
String [] [] rec = new string [8] [8];
Int [] A = new int [8];
Int [] B = new int [15];
Int [] C = new int [15];
Int sum;
Public eightqueue (){
Super ();
For (INT I = 0; I <this. Rec. length; I ++ ){
For (Int J = 0; j <this. rec [I]. length; j ++ ){
This. rec [I] [J] = "○ ";
}
}
}
Public void PRT (){
System. Out. println ("");
For (INT I = 0; I <this. Rec. length; I ++ ){
For (Int J = 0; j <this. rec [I]. length; j ++ ){
System. Out. Print (this. rec [I] [J] + "");
}
System. Out. println ("");
}
System. Out. println ("");
}
/**
* Set the queen of line I
*
* @ Param I
*/
Void Qu (int I ){
For (INT icolumn = 0; icolumn <8; icolumn ++ ){
If (A [icolumn] = 0 & B [I-icolumn + 7] = 0
& C [I + icolumn] = 0 ){
// Do not conflict
REC [I] [icolumn] = "● ";
A [icolumn] = 1;
B [I-icolumn + 7] = 1;
C [I + icolumn] = 1;
If (I <7)
Qu (I + 1 );
Else {
// Sysout
PRT ();
Sum ++;
}
// Whatever how to put the queen, mission is impossible. rollback
REC [I] [icolumn] = "○ ";
A [icolumn] = 0;
B [I-icolumn + 7] = 0;
C [I + icolumn] = 0;
}
}
}
/**
* 8 queen
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Eightqueue eq = new eightqueue ();
Eq. Qu (0 );
System. Out. println (Eq. Sum );
}
}
Algorithm 2 does not know where it was plagiarized. The algorithm does not ultimately agree to the permutation and combination, but only provides many combinations, but the algorithm is indeed very subtle, to share with you.
Package org. luyang. csdn;
Public class queen {
Static int sum = 0, upperlim = 1;
Private Static void test (INT row, int lD, int rd ){
If (row! = Upperlim ){
Int Pos = upperlim &~ (Row | LD | rd );
While (Pos! = 0 ){
Int P = POS &-Pos;
Pos-= P;
Test (row + P, (LD + p) <1, (RD + p)> 1 );
}
} Else
Sum ++;
}
Public static void main (string [] ARGs ){
Int n = 8;
If (ARGs. Length = 1)
N = integer. parseint (ARGs [0]);
Long TM = system. currenttimemillis ();
If (n <1) | (n> 32 )){
System. Out. println ("Heh .. I can't calculate that .");
System. Exit (-1 );
}
System. Out. println (n + "Queens ");
Upperlim = (upperlim <n)-1;
Test (0, 0, 0 );
System. Out. println ("number of solutions is" + sum + ","
+ (System. currenttimemillis ()-TM) + "milliseconds ");
}
}