八皇后问题
来源:互联网 发布:vb中index 编辑:程序博客网 时间:2024/05/29 09:05
#include<iostream>
#include<math.h>
#include<math.h>
using namespace std;
int a[8], flag = 1, c= 1;
void search(int row);
int complies(int row, int cot);
void output();
int a[8], flag = 1, c= 1;
void search(int row);
int complies(int row, int cot);
void output();
int main(void)
{
search(0);
system("pause");
return 0;
}
{
search(0);
system("pause");
return 0;
}
void search(int row)
{
if (row == 8)
{
cout << "No" << ' '<<c << ':' << endl;
output();
c++;
}
else
{
for (int cot = 0; cot < 8; cot++)
{
if (complies(row, cot) == 1)
{
a[row] = cot;
search(row + 1);
}
}
}
}
{
if (row == 8)
{
cout << "No" << ' '<<c << ':' << endl;
output();
c++;
}
else
{
for (int cot = 0; cot < 8; cot++)
{
if (complies(row, cot) == 1)
{
a[row] = cot;
search(row + 1);
}
}
}
}
int complies(int row, int cot)
{
flag = 1;
for (int i = 0; i < row; i++)
{
if (a[i] == cot || abs(row - i) == abs(a[i] - cot))
flag = 0;
}
return flag;
}
{
flag = 1;
for (int i = 0; i < row; i++)
{
if (a[i] == cot || abs(row - i) == abs(a[i] - cot))
flag = 0;
}
return flag;
}
void output()
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (j == a[i]) cout << 'A';
else cout << '.';
}
cout << endl;
}
}
{
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (j == a[i]) cout << 'A';
else cout << '.';
}
cout << endl;
}
}
阅读全文
0 0
- 八皇后 n皇后 问题
- 八皇后N皇后问题
- 八皇后问题
- 八皇后问题
- 八皇后问题
- 八皇后问题(2)
- 八皇后问题(3)
- 八皇后问题
- 八皇后问题程序
- 浅谈八皇后问题
- 八皇后问题
- 八皇后问题
- 八皇后问题
- 八皇后问题
- 八皇后问题求解
- 八皇后问题
- 八皇后问题
- 八皇后问题
- python类与对象part1
- Python pickle模块学习(超级详细)
- Lintcode116 Jump Game solution 题解
- Mysql数据类型---SET
- SpringData详解
- 八皇后问题
- Mysql数据类型---ENUM
- ARCore:从哪里冒出来的ARCore
- makefile 与 链接库
- 使用AdBlockPlus在App中过滤网页广告
- 简单靶点
- 爬虫学习
- Android 混淆详解
- AS出现Error:Cause: peer not authenticated