八皇后问题_____递归线性搜索
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");
}
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");
}
相关文章推荐
- 八皇后问题 (递归 搜索)
- 递归与分治策略-2.9.1线性时间选择(随机划分基准)(第k小问题)
- [置顶] 递归与分治策略-2.9.2线性时间选择(取中位数的中位数基准)(第k小问题)
- //回溯法搜索八皇后问题
- 暴力搜索 回溯法 —— 八皇后问题
- 八皇后问题 递归解法
- 八皇后问题递归解发
- 01背包问题(当有的背包重量是非整数时)的递归(优化成动态规划+再用滚动数组优化)解法+一些动态规划(递归,搜索)的高级技巧
- 利用递归求解八皇后问题
- 20150410 递归实现八皇后问题
- 八皇后问题(典型的递归回溯)
- 【数据结构与算法】八皇后问题之递归
- 关于递归求解八皇后问题
- 递归和分治思想4|八皇后问题 - 数据结构和算法34
- 高效的八皇后问题非递归解法
- 递归--基于回溯和递归的八皇后问题解法
- 八皇后问题(递归版)
- 递归,八皇后问题
- 局部搜索思想求解八皇后问题
- 最大子数组问题的递归和非递归(线性时间)代码