#include<iostream>
using namespace std;
const int N = 8;
int path[N];
void find(int k) //find position in line k
{
if(k==N)
{
for(int i = 0; i < N; i++)
cout << path[i] << " ";
cout << endl;
}
for(int i = 0; i < N; i++) // test from column i, so (k,i)
{
int accept=1;
for(int t = 0; t < k; t++) // test if compete with the already fixed position
{
if(abs(t-k)==abs(path[t]-i) || t==k || path[t]==i)
{
accept=0;
break;
}
}
if(accept==1)
{
path[k]=i;
find(k+1);
}
}
}
int main()
{
find(0);
}
八皇后
最新推荐文章于 2023-07-07 14:04:01 发布