vjudge submit link
Title: Eight Queens
——Those who know how to play chess know very well: the queen can take other pieces on the horizontal, vertical, and diagonal lines without a limit. How to put 8 queens on the board (there are 8 * 8 squares) so that none of them can be eaten! This is the famous Eight Queens problem.
——For a placement method of 8 queens that meets the requirements, define a queen string a to correspond to it, that is, a=b1b2...b8, where bi is the number of columns where the i-th queen in the corresponding placement method is located. It is known that there are 92 sets of solutions for the 8 queen problem (that is, 92 different queen strings).
—— Given a number b, the b-th string is required to be output. The comparison of strings is as follows: the queen string x is placed before the queen string y, and is smaller than y if and only if x is treated as an integer.
Input
line 1 is the number of groups n of test data, followed by n lines of input. Each group of test data occupies 1 row, including a positive integer b (1 <= b <= 92)
Output
has n rows, and each row of output corresponds to one input. The output should be a positive integer, the queen string corresponding to b.
Sample Input
2
1
92
Sample Output
15863724
84136275
Reference: Classic N Queen
Code:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=92+10;
const int M=10;
char s[N][M];
int a[10];
int cnt=1;
int judge(int x)//传入x行a[x]列,判断该位置是否能下//根据规则,同列的不能下(同行不可能),45度倾斜不能下
{
int i,dd1,dd2;
for(i=1;i<x;i++)
{
dd1=abs(x-i);
dd2=abs(a[x]-a[i]);
if(dd1==dd2||a[x]==a[i])
return 0;
}
return 1;
}
void dfs(int x)//棋子打算放在x行上
{
int i,j;
if(x==8+1)
{
for(i=1;i<=8;i++)
s[cnt][i]=a[i]+'0';
cnt++;
return;
}
for(j=1;j<=8;j++)
{
a[x]=j;//打算下在x行j列,具体能不能下还需下面的判断
if(judge(x)==0)//表示该位置不能下
continue;
dfs(x+1);
}
}
void init()
{
cnt=1;
dfs(1);
}
int main()
{
init();//dabiao
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
printf("%s\n",s[n]+1);
}
return 0;
}