wisdom2010 @ 2024-07-18 16:22:14
#include<bits/stdc++.h>
using namespace std;
int n;
const int maxn = 15;
int dis[maxn][maxn] = {0};
int ans = 0;
struct Q
{
int x;
int y;
}q[maxn];
void dfs(int step)
{
if(step == n)
{
ans++;
if(ans <= 3)
{
for(int i = 0; i < n; i++)
printf("%d ", q[i].x + 1);
printf("\n");
}
return ;
}
for(int i = 0; i < n; i++)
{
if(dis[step][i] == -1)
continue;
q[step].x = i;
q[step].y = step;
for(int j = 0; j < n; j++)//标记行和列
{
dis[step][j] = -1;
dis[j][q[step].x] = -1;
}
for(int j = 1; j <= max(q[step].x - 1, n - q[step].x); j++)//标记对角线
{
if(q[step].x - j >= 0 && q[step].y - j >= 0)
dis[q[step].x - j][q[step].y - 1] = -1;
if(q[step].x + j <= n && q[step].y + j <= n)
dis[q[step].x + j][q[step]. y + j] = -1;
}
dfs(step + 1);
dis[step][q[step].x] = 0;//消除标记
for(int j = 0; j < n; j++)
{
dis[step][j] = 0;
dis[j][q[step].x] = 0;
}
for(int j = 1; j <= max(q[step].x - 1, n - q[step].x); j++)
{
if(q[step].x - j >= 0 && q[step].y - j >= 0)
dis[q[step].x - j][q[step].y - 1] = 0;
if(q[step].x + j <= n && q[step].y + j <= n)
dis[q[step].x + j][q[step]. y + j] = 0;
}
}
}
int main()
{
scanf("%d", &n);
dfs(0);
printf("%d", ans);
return 0;
}
by wisdom2010 @ 2024-07-18 19:31:08
@An_Idiot 你太谦虚了(^_^)
by An_Idiot @ 2024-07-18 19:36:36
@wisdom2010 emm...我看你这个代码逻辑有点混乱,不妨问一下你学了多久了?
by An_Idiot @ 2024-07-18 19:38:57
@wisdom2010 你在做题的前可以把你想到的答题步骤记录下来,再用代码实现,这样会好很多。
by wisdom2010 @ 2024-07-18 19:59:41
@An_Idiot 额...私信