四年级小朋友的思路
查看原帖
四年级小朋友的思路
924748
Farmer_Jhon_Bessie楼主2024/8/6 14:12
#include<bits/stdc++.h>
using namespace std;
int n;
int a[15][15];
vector<int> ans;
int cnt = 0;
void attack(int x, int y, int v) {
	for (int i = 1; i <= n; i++) a[x][i] += v;
	for (int i = 1; i <= n; i++) a[i][y] += v;
	for (int i = x, j = y; i >= 1 && j >= 1; i--, j--) a[i][j] += v;
	for (int i = x, j = y; i <= n && j >= 1; i++, j--) a[i][j] += v;
	for (int i = x, j = y; i <= n && j <= n; i++, j++) a[i][j] += v;
	for (int i = x, j = y; i >= 1 && j <= n; i--, j++) a[i][j] += v;
	a[x][y] -= (5 * v); // 抵消前面多加的5次
}
void dfs(int dep) {
	if (dep > n) {
		if (++cnt <= 3) {
			for (int i : ans) cout << i << " ";
			cout << endl;
		}
		return;
	}
	for (int i = 1; i <= n; i++) {
		if (a[dep][i]) continue;
		ans.push_back(i);
		attack(dep, i, 1);
		dfs(dep + 1);
		ans.pop_back();
		attack(dep, i, -1);
	}
}
int main() {
	cin >> n;
	dfs(1);
	cout << cnt;
	return 0;
}
2024/8/6 14:12
加载中...