zl程序教程

您现在的位置是:首页 >  其它

当前栏目

八皇后

皇后
2023-09-14 09:06:47 时间

 

#include<iostream>
const int maxN = 8;
int map[maxN], cnt = 0;
bool check(const int &x) {
	for (int i = 0; i < x; ++i)
		if (map[x] == map[i] || x - i == map[x] - map[i] || x - i == map[i] - map[x])return false;
	return true;
}
void dfs(int pos) {
	if (pos == maxN) {
		++cnt;
		printf("no.%d\n", cnt);
		for (int i = 0; i < maxN; ++i)
			for (int j = 0; j < maxN; ++j) {
				printf("%d", map[i] == j ? 1 : 0);
				if (j == maxN - 1)printf("\n");
				else printf(" ");
			}
		printf("\n");
		return;
	}
	for (int i = 0; i < maxN; ++i) {
		map[pos] = i;
		if (!check(pos))continue;
		dfs(pos + 1);
	}
}
int main() {
	dfs(0);
	printf("%d\n", cnt);
	return 0;
}