用C++如何实现八皇后问题
作者:bea
从第一行开始逐个检索安全位置摆放皇后,一旦有安全位置则考虑下一行的安全位置。如果发现某行没有安全位置,则返回上一行继续检索安全位置;如果发现在最后一行找到了安全位置则输出整个棋盘。
原理很简单,整个程序中表现了这个思想的函数是void Solve()
下面是实现的代码:
//C++实现八皇后问题代码
#include
#include
#include
const int N=8; // Of course we are able to solve the "N queens problem".
using namespace std;
class QueenChess
{
public:
QueenChess(); // To initialize the chessboard
void Solve(); // To solve the problem and calculate the number of solutions
private:
string chessBlank; // A row of blank chessboard. We use this to 'delete' the row we don't need. (in fact, we substitute the black row for the row we want to delete)
string chessState[N]; // The whole chessboard, which has N rows and N columns
int solves; // The number of solutions
bool SafeJudge(int row, int col) const; // To judge whether we can put our queen in a paiticular position (row, col)
void PlaceQueen(int row); // Put our queen in a safe place in a particular row
void DrawChess() const; // Draw the whole chessboard when we got a solution
};
QueenChess::QueenChess()
{
solves=0;
for (int i=0;i
{
chessBlank+="-"; // So, our original row is just like "--------"
}
for (int i=0; i
{
chessState[i]=chessBlank; // Then, it becomes N rows
}
}
void QueenChess::Solve()
{
PlaceQueen(0); // Start placing queens at the first row
cout
}
void QueenChess::PlaceQueen(int row)
{
for (int col=0;col
{
if (SafeJudge(row,col)==true) // If we find one
{
chessState[row][col]='Q'; // We change the '-' by 'Q'
if (row
{
PlaceQueen(row+1); // If we found a safe place in this row, we do the same in the next row
}
else // We place N queens in the chessboard successfully
{
solves++; // solves++
DrawChess(); // Everytime we got a solution, we output it
}
}
chessState[row]=chessBlank; // Backtracking method. If we failed the find a safe place in the nth row, we just clear this row and go back to the previous row. Then we will find a new safe place with a bigger No. of column. If we can't find such a place, just go back to a previous previous row.
}
}
bool QueenChess::SafeJudge(int row,int col) const
{
int rowChecker, colChecker; // Two variables for checking
for (rowChecker=0; rowChecker
{
string rowState=chessState[rowChecker]; // check the invaild region made by the queen of the rowCheckerth row
colChecker=rowState.find('Q'); // Use 'find' to get the number of column of the queen
if (row==rowChecker or col==colChecker or (rowChecker-colChecker)==(row-col) or (rowChecker+colChecker)==(row+col)) // Check whether the place is safe
{
//cout
cout
for (drow=0;drow
{
cout
}
int main()
{
QueenChess chess;
chess.Solve();
return 0;
}
原文地址:http://soft.chinabyte.com/database/383/12071383.shtml
猜你喜欢
您可能感兴趣的文章:
- asp.net实现页面返回代码
- asp.net URL重写实现动态页面静态化
- 采用WebClient 并以post方式发送数据
- FtpWebRequest 实现FTP常用功能详解
- ashx文件和aspx文件有什么区别
- 如何选择website还是web application,哪个好
- asp.net怎样提高首页性能
- 怎样在C#中执行Javascript代码
- 企业市场将是微软的另一条战线
- UML正逐渐下滑的13个理由
- .NET怎样实现 MVC页面返回不同类型的内容
- C#计算文件的MD5值实例
- DateTime日期类型格式化显示
- .NET异常处理最佳实践方案
- 浅谈C++中const类型变量和函数重载
- 什么是C++中的虚拟克隆
- C++是什么《我的第一本C++书》
- C++中char*,String,int,CString间转换
- 解决C++客户端到C#服务器中文乱码