openjudge-NOI 2.5-1756 八皇后
题目链接:http://noi.openjudge.cn/ch0205/1756/
题解:
上一道题稍作改动……
1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 bool a[9][9]; 5 int num,s[93]; 6 void print() 7 { 8 for(int i=1;i<=8;i++) 9 { 10 for(int j=1;j<=8;j++) 11 { 12 if(a[i][j]) 13 { 14 s[num]*=10; 15 s[num]+=j; 16 } 17 } 18 } 19 } 20 int check(int x,int y) 21 { 22 int tmp1,tmp2; 23 tmp1=1;tmp2=y-x+1; 24 for(;tmp1<=x;tmp1++,tmp2++) 25 { 26 if(tmp2>=1&&tmp2<=8&&a[tmp1][tmp2]==true)return 0; 27 } 28 tmp1=1;tmp2=y+x-1; 29 for(;tmp1<=x;tmp1++,tmp2--) 30 { 31 if(tmp2>=1&&tmp2<=8&&a[tmp1][tmp2]==true)return 0; 32 } 33 tmp1=1;tmp2=y; 34 for(;tmp1<=x;tmp1++) 35 { 36 if(tmp2>=1&&tmp2<=8&&a[tmp1][tmp2]==true)return 0; 37 } 38 return 1; 39 } 40 void dfs(int dep) 41 { 42 if(dep==9) 43 { 44 num++; 45 print(); 46 return; 47 } 48 for(int i=1;i<=8;i++) 49 { 50 if(dep==1||check(dep,i)) 51 { 52 a[dep][i]=true; 53 dfs(dep+1); 54 a[dep][i]=false; 55 } 56 } 57 } 58 int main() 59 { 60 dfs(1); 61 sort(s+1,s+93); 62 int m,n; 63 scanf("%d",&n); 64 while(n--) 65 { 66 scanf("%d",&m); 67 printf("%d\n",s[m]); 68 } 69 return 0; 70 }