N Queen's questionTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 10239 Accepted Submission (s): 4609
Problem description placed n Queens on the N*n's checkered chessboard, making them not attack each other (that is, any 2 queens are not allowed to be in the same row, in the same column, or in a diagonal line with a 45-angle checkerboard border.)
Your task is to find out how many legal placement methods are available for a given n.
Input has several lines, one positive integer n≤10 per line, indicating the number of boards and queens, or, if n=0, the end.
Output has several rows, one positive integer per line, representing the number of different placements of the queen corresponding to the input row.
Sample Input
1850
Sample Output
19210
Authorcgf
The code explains everything code:
#include <stdio.h> #define MAX 15int n, ans = 0, res[max],map[max];void DFS (int row) {if (row = n) {ans++; return;} for (int i = 0; i < n; ++i) {bool flag = false; Map[row] = i, for (int j = 0; j < row; ++j) {if (map[row]==map[j]| | map[row]-row==map[j]-j| | MAP[ROW]+ROW==MAP[J]+J) {flag = true; break;}} if (!flag) {DFS (row+1);}}} int main () {for (int i = 1; i <=; ++i) {n = i; ans = 0;D fs (0); Res[i] = ans;} while (~SCANF ("%d", &n) && N) {printf ("%d\n", Res[n]);} return 0;}
with June
HDU 2553 N Queen problem Classic search, DFS solution