N Queen's question
Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16803 Accepted Submission (s): 7631
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 Input1850
Sample Output19210 Backtrack plus DFS
#include <stdio.h>#include<math.h>intx[ the],y[ the]={0};intSum,n;intPlaceintk) { inti; for(i=1; i<k;i++) { if(ABS (K-i) ==abs (x[k]-x[i]) | | x[k]==X[i])return 0; } return 1;}voidDFS (inta) { inti; if(a>N) Sum++; Else for(i=1; i<=n;i++) {X[a]=i; if(Place (a)) DFS (a)+1); }}intMain () {inti,j,n1; for(i=1; i<=Ten; i++) {n=i; Sum=0; DFS (1); Y[i]=sum; } while(SCANF ("%d", &n1) = =1&&N1) {printf ("%d\n", y[n1]); } return 0;}
hdu2553 N Queen Question