36求调
查看原帖
36求调
707007
allowing楼主2024/7/9 21:01
#include <iostream>
using namespace std;
const int N = 50;
int n;
int tt, a[N];
bool b[N], c[N], d[N];
//a[x] = y 第x行第y列有皇后
//b[y] = 1 第y列有皇后 
//c[x+y] 第一个对角线
//d[x-y+n]第二个对角线
int ans[100][10];
void dfs(int x){
	if (x > n){
		tt++;
		for (int i = 1; i <= n; i++) ans[tt][i] = a[i];
		return ;
	}
	
	for (int y = 1; y <= n; y++){
		if (!b[y] && !c[x + y] && !d[x - y + n]){
			a[x] = y, b[y] = c[x + y] = d[x - y + n] = 1;
			dfs(x + 1);
			b[y] = c[x + y] = d[x - y + n] = 0;
		}
	}
} 
int main(){
	
	cin >> n;
	dfs(1);
	for (int i = 1; i <= 3; i++) {
		for (int j =1 ; j <= n; j++) 
			cout << ans[i][j] << ' ';
		puts("");
	}
	
	cout << tt;
	
	return 0;
}
2024/7/9 21:01
加载中...