您的位置:首页 > 其它

八皇后问题_____递归线性搜索

2009-03-03 22:47 239 查看
# include<iostream>
using namespace std;
bool b[27],c[27],d[27];
int a[9],i;
void print()
{
int i1;
i++;
cout<<i<<" ";
for (i1=1;i1<=8;i1++)
cout<<a[i1]<<' ';
cout<<endl;
}
void sou(int i)
{
for (int i1=1;i1<=8;i1++)
if (b[i1]&&c[i+i1]&&d[i-i1+8])//运用直线方程求解 ,
{
a[i]=i1;
b[i1]=false;
c[i+i1]=false;
d[i-i1+8]=false;
if (i<8) sou(i+1);
else print();//输出
b[i1]=true;//释放
c[i+i1]=true;
d[i-i1+8]=true;
}
}
int main()
{
for (int o=1;o<=27;o++)//初始化变量
{
b[o]=true;c[o]=true;d[o]=true;
}
i=0;
sou(1);
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: