面向对象思想中的八皇后问题
2014-04-09 17:44
225 查看
一直都是用深搜求八皇后问题,昨天面向对象老师讲了一下用面向对象思想来写,感到质疑,回来写了一下,感觉恶心。
这是人类的思想吗?
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
#define N 8
class Queen
{
const int col;
int row;
bool testAttack(int testrow, int testcol)
{
if(testrow == row)return true;
if(abs(testrow-row) == abs(testcol - col))return true;
return false;
}
public:
Queen(int c, int r, Queen *n):col(c), row(r), neighbor(n){};
void show()
{
printf("row = %d col = %d\n", row, col);
if(neighbor) neighbor->show();
}
Queen *neighbor;
bool findsolution();
bool canAttack(int , int);
bool advance();
};
bool Queen::findsolution()
{
while(neighbor && neighbor->canAttack(row, col))
{
if(!advance())
return false;
}
return true;
}
bool Queen::advance()
{
while(row < N)
{
row ++;
return findsolution();
}
if(neighbor && !neighbor->advance())
return false;
if(row == N && col == 1)return false;
row = 1;
return findsolution();
}
bool Queen::canAttack(int testrow, int testcol)
{
if(testAttack(testrow, testcol))
return true;
return neighbor && neighbor->canAttack(testrow, testcol);
}
int main()
{
Queen *lastqueen = 0;
for(int i = 1; i <= N; i++)
{
lastqueen = new Queen(i, 1, lastqueen);
if(!lastqueen->findsolution())printf("nosolution\n");
else if(i == N)
{
lastqueen->show();
while(lastqueen->advance())lastqueen->show();
}
}
return 0;
}
这是人类的思想吗?
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<cmath>
#include<string>
using namespace std;
#define N 8
class Queen
{
const int col;
int row;
bool testAttack(int testrow, int testcol)
{
if(testrow == row)return true;
if(abs(testrow-row) == abs(testcol - col))return true;
return false;
}
public:
Queen(int c, int r, Queen *n):col(c), row(r), neighbor(n){};
void show()
{
printf("row = %d col = %d\n", row, col);
if(neighbor) neighbor->show();
}
Queen *neighbor;
bool findsolution();
bool canAttack(int , int);
bool advance();
};
bool Queen::findsolution()
{
while(neighbor && neighbor->canAttack(row, col))
{
if(!advance())
return false;
}
return true;
}
bool Queen::advance()
{
while(row < N)
{
row ++;
return findsolution();
}
if(neighbor && !neighbor->advance())
return false;
if(row == N && col == 1)return false;
row = 1;
return findsolution();
}
bool Queen::canAttack(int testrow, int testcol)
{
if(testAttack(testrow, testcol))
return true;
return neighbor && neighbor->canAttack(testrow, testcol);
}
int main()
{
Queen *lastqueen = 0;
for(int i = 1; i <= N; i++)
{
lastqueen = new Queen(i, 1, lastqueen);
if(!lastqueen->findsolution())printf("nosolution\n");
else if(i == N)
{
lastqueen->show();
while(lastqueen->advance())lastqueen->show();
}
}
return 0;
}
相关文章推荐
- 黑马程序员__用面向对象思想分析问题小结
- 由一个问题看JAVA面向对象思想(一)
- 面向对象思想解决农场养牛问题
- [黑马程序员]11[OC语言]圆,点问题, OC的面向对象程序设计思想和类封装
- Java面向对象思想解决猜拳问题
- 面向对象思想解决的问题以及面向对象的三个特征
- 6 Java基础语法(二维数组,参数传递与数据加密问题)&面向对象(面向对象思想,类与对象及使用,对象内存图解)
- poj 1007 Java AC啦(面向对象思想)
- 面向对象程序设计思想简述
- 黑马程序员——JAVA基础--面向对象思想
- 黑马程序员-java面向对象问题分析
- UML学习笔记(三):运用面向对象思想
- Java基础与面向对象思想
- 面向对象的设计方式处理Android编程中的GridView问题,以及自定义ListAdapter的使用
- Java面向对象思想
- java面向对象思想笔记
- c#中面向对象思想的理解
- java面向对象思想引入,类定义的格式
- 第四节:详细讲解Java中的类和面向对象思想
- .使用面向对象思想,输出员工信息并计算员工的工资。实现过程为:定义一个部门(Department)类,该类有部门编号(Id)、部门名称(Name)属性; 再定义一个员工(Employee)类,该类的属