求助
查看原帖
求助
1020531
liuyiyang_khaili楼主2024/1/14 09:51
#include<bits/stdc++.h>
using namespace std;
int n;
int cnt;
bool mp[17][17];
vector<int>t;
vector<int>ans[3];
bool p = 0;
void makeans() {
	for (int i = 0; i < n; i++) {
		ans[cnt].push_back(t[i]);
	}
}
void noset(int x,int y) {
	mp[x][y] = 0;
	for (int i = 1; i <= n; i++) {//行与列
		mp[x][i] = 0;
		mp[i][y] = 0;
	}
	int i = x - 1, j = y - 1;
	while (i >= 1 && j >= 1) {//左上斜线
		mp[i][j] = 0;
		i--;
		j--;
	}
	i = x + 1;
	j = y + 1;
	while (i <= n && j <= n) {//右下斜线
		mp[i][j] = 0;
		i++;
		j++;
	}
	i = x - 1;
	j = y + 1;
	while (i >= 1 && j <= n) {//右上斜线
		mp[i][j] = 0;
		i--;
		j++;
	}
	i = x + 1;
	j = y - 1;
	while (i <= n && j >= 1) {//左下斜线
		mp[i][j] = 0;
		i++;
		j--;
	}
}
void sett(int x,int y) {
	mp[x][y] = 1;
	for (int i = 1; i <= n; i++) {
		mp[x][i] = 1;
		mp[i][y] = 1;
	}
	int i = x - 1, j = y - 1;
	while (i >= 1 && j >= 1) {//左上斜线
		mp[i][j] = 1;
		i--;
		j--;
	}
	i = x + 1;
	j = y + 1;
	while (i <= n && j <= n) {//右下斜线
		mp[i][j] = 1;
		i++;
		j++;
	}
	i = x - 1;
	j = y + 1;
	while (i >= 1 && j <= n) {//右上斜线
		mp[i][j] = 1;
		i--;
		j++;
	}
	i = x + 1;
	j = y - 1;
	while (i <= n && j >= 1) {//左下斜线
		mp[i][j] = 1;
		i++;
		j--;
	}
}
void dfs(int x) {
	if (x > n) {
		if (cnt >= 3) {
			cnt++;
		}
		else {
			makeans();
			cnt++;
		}
		return;
	}
	for (int i = 1; i <= n; i++) {
		if (mp[x][i] == 0) {
			if (!p) {
				cout << x << ' ' << i << '\n';
				p = 1;
			}
			sett(x, i);
			t.push_back(i);
			dfs(x + 1);
			t.pop_back();
			noset(x, i);
		}
	}
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	memset(mp, 0, sizeof(mp));
	cin >> n;
	dfs(1);
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j < n; j++) {
			cout << ans[i][j] << ' ';
		}
		cout << '\n';
	}
	cout << cnt;
	return 0;
}``````
2024/1/14 09:51
加载中...