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

来源:互联网 发布:身高155的网红的淘宝店 编辑:程序博客网 时间:2024/05/16 01:55

 

# 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");
}

原创粉丝点击