#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
int a[8];int c = 0;
int n = 4;
bool flag(int pos) {
int i;
for (i = 0; i < pos; ++i) {
if (a[i] == a[pos]) { return false; }
if (a[i] == a[pos] - (pos - i) || a[i] == a[pos] + (pos - i))
{ return false; }
}
return true;
}
void uotputc()
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j == a[i])
printf("%c ", 2);
else
printf("* ");
}
printf("\n");
}
}
void start(int pos) {
int i;
for (i = 0; i < n; ++i) {
a[pos] = i;
if (flag(pos))
{
if (pos == n-1)
{
c++;
uotputc();
printf("\n");
}
else { start(pos + 1); }
a[pos] = -1;
}
}
return;
}
int main444() {
int i;
for (i = 0; i < n; i++)
a[i] = -1;
start(0);
cout << c << "种方法";
system("pause");
return 0;
}
C 数据算法 八皇后
最新推荐文章于 2023-12-29 12:49:32 发布