N queen's question
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 5563 accepted submission (s): 2518
Problem description places n queens on the square board of N * n so that they do not attack each other (that is, two queens are not allowed to be in the same row, 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.
There are several input rows. Each row has a positive integer of N ≤ 10, indicating the number of the Board and the Queen. If n = 0, it indicates the end.
There are several rows in output. 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# Include <iostream> using namespace STD; # include "stdio. H" # define Max 12int VST [Max]; int CNT [Max]; int ans; int N; bool flag; // VST [I] indicates the number of columns of Queen I // Ans stores the number of current schemes and is used to copy to ans [I]. N indicates the number of Queen's loops. // Flag indicates whether the position can be placed in the Queen. If yes, It is truevoid DFS (INT row) // Deep Search {int I, j; If (ROW = n + 1) // if the number of Queen s is greater than the required number N, this solution is feasible, ANS + 1 Ans ++; else for (I = 1; I <= N; I ++) // The previous cycle is also required for this queen. First, select a location to see how many can be placed below. If not, replace {flag = true; VST [row] = I; for (j = 1; j <row; j ++) // compare the previous queens of the Queen, check whether the condition {If (VST [row] = VST [J] | VST [row]-row = VST [J]-j | VST [row] + row = VST [J] + J) //{ // Flag = false; break ;}} if (FLAG) DFS (row + 1) cannot be placed in the same diagonal line or column ); // If it can be put, consider placing the next queen, row + 1} int main () {int I; for (n = 1; n <11; n ++) // 'from 1 ~ 10 first calculate the various schemes, and then directly output {ans = 0; DFS (1 ); // Search CNT [N] = ans from 1 each time; // CNT [I] indicates the number of feasible solutions for putting I Queen} while (scanf ("% d ", & N )! = EOF & N) {printf ("% d \ n", CNT [N]);} return 0 ;}