求调(玄关)
查看原帖
求调(玄关)
1251525
wisdom2010楼主2024/7/18 16:22
#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;
}
2024/7/18 16:22
加载中...