http://ac.jobdu.com/problem.php?id=1140
直接用的USACO上代码,不解释
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 int N=8;
5 bool placed[15][15]={false};
6 int diagonals_l[50]={0};
7 int diagonals_r[50]={0};
8 bool column_ok[15]={true};
9 int solutions[93]={0};
10 int cur=1;
11 bool can_place_queen(int row,int column)
12 {
13 int r=row-column;
14 if(r<0){
15 r=-1*r+N;
16 }
17 if(column_ok[column]&&!placed[row][column]&&diagonals_l[row+column]<1&&diagonals_r[r]<1){
18 return true;
19 }
20 return false;
21
22 }
23 void place_queen(int row)
24 {
25 if(row==N+1){
26 /*deal with output :)
27 */
28 int i,j;
29 for(i=1;i<=N;i++){
30 for(j=1;j<=N;j++){
31 if(placed[i][j]){
32 solutions[cur]=solutions[cur]*10+j;
33 break;
34 }
35 }
36 }
37 cur++;
38 }
39
40 int column;
41 for(column=1;column<=N;column++){
42 if(can_place_queen(row,column)){
43 int l=row+column;
44 int r=row-column;
45 if(r<0){
46 r=-1*r+N;
47 }
48 column_ok[column]=false;
49 placed[row][column]=true;
50 diagonals_l[l]++;
51 diagonals_r[r]++;
52 place_queen(row+1);
53 diagonals_l[l]--;
54 diagonals_r[r]--;
55 placed[row][column]=false;
56 column_ok[column]=true;
57 }
58 }
59 }
60 int main()
61 {
62 memset(column_ok,true,sizeof(column_ok));
63 place_queen(1);
64 int t;
65 while(scanf("%d",&t)!=EOF){
66 while(t--){
67 int n;
68 scanf("%d",&n);
69 printf("%d\n",solutions[n]);
70 }
71 }
72 }