N queen's question Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 8525 accepted submission (s): 3802
Problem description There are n queens on the N * n checkboard so that they do not attack each other (that is, two queens are not allowed to be in the same row, in the same column, it is not allowed to be on a diagonal line with 45 corners of the checker border.
Your task is to determine the number of valid placement methods for the given n.
Input There are several rows in total. Each row has a positive integer n ≤ 10, indicating the number of the Board and the Queen. If n = 0, it indicates the end. Output There are several rows in total, each row has a positive integer, indicating the number of different places corresponding to the queen of the input row. Sample Input
1850
Sample output
19210
Algorithm analysis: Starting from placing the first queen, keep searching for all feasible solutions and output 10 results in advance. Measure the test taker's knowledge about the diagonal line of each queen. (ABS (k-I) = ABS (X [k]-X [I]))-> For the two queens on a diagonal line, the abscissa difference must be equal to the ordinate difference.
# Include <stdio. h> // Add backtracing to the table # include <math. h> int X [15], Y [15] = {0}; int sum, N; int place (int K) {int I; for (I = 1; I <K; I ++) {If (ABS (k-I) = ABS (X [k]-X [I]) | x [k] = x [I]) // pruning is used to determine whether conditions are met. I indicates the number of rows where the queen is located, X [I] indicates the number of columns, // Therefore, the preceding condition is used to determine whether two queens are on the diagonal line and whether they are on the same column. // The number of rows does not need to be determined, because their own I represents the number of rows return 0;} return 1;} void DFS (int A) {int I; if (A> N) sum ++; elsefor (I = 1; I <= N; I ++) {x [a] = I; // Number of columns placed by Queen a if (place (a) // determine whether the row can be placed in DFS (a + 1 ); // if possible, place the next queen.} int main () {int I, j, N1; for (I = 1; I <= 10; I ++) {n = I; // represents the sum of several queens = 0; // each time the number is set to 0 DFS (1 ); // start from the first queen every time. Y [I] = sum;} while (scanf ("% d", & N1) = 1 & N1) {printf ("% d \ n", Y [N1]);} return 0 ;}