我想找到可能的方法来在棋盘上设置5个皇后的方法,但他们不能互相攻击。我已经成功找到第一套。问题是我将如何能够找到5个皇后的下一组职位。在我的程序的过程是这样的:在8x8国际象棋棋盘上的5个皇后
- 基于电路板上
- 回路的电流皇后通过板上的所有位置
- 检查当前的位置是一个禁止位置的矢量主板上的不允许位置
- 如果不是,返回的位置,将其添加到皇后区的载体板上,并开始处理再次
继续,直到有没有更多的对可用的即osition所有其余的位置是不允许
#include <iostream>
#include <vector>
using namespace std;
const int BSIZE = 8;
char chessBoard[BSIZE][BSIZE];
struct qPos
{
qPos() : h(0), v(0), found(true) {}
int h; //horizontal pos
int v; //vertical pos
bool found; //if position is available
};
qPos findNextQPos(vector<qPos> Qs);
void fillBoard(vector<qPos> Qs);
void print();
vector<qPos> generateDisallowed(vector<qPos> Qs);
bool isDisallowed(qPos nextPos, vector<qPos> disallowedPos);
int main(int argc, char **argv){
vector<qPos> QsOnBoard; //Position of all the queens on board
qPos nextQ; //next possible position
while (nextQ.found)
{
nextQ = findNextQPos(QsOnBoard);
if (nextQ.found)
{
QsOnBoard.push_back(nextQ); //If the nextQ is available i.e. not disallowed, add it to the queens vector
}
}
fillBoard(QsOnBoard); //Fill the board with queens positions
print(); // print the board
return 0;
}
qPos findNextQPos(vector<qPos> Qs) {
// Generate disallowed positions based on all the queens on board
vector <qPos> disallowedPos = generateDisallowed(Qs);
qPos nextQ;
for (size_t i = 0; i < BSIZE; i++)
{
for (size_t j = 0; j < BSIZE; j++)
{
nextQ.h = i;
nextQ.v = j;
if (!isDisallowed(nextQ, disallowedPos)) { //Check if next possible position is a disallowed position
//cout << "Next available:\n" << nextQ.h << ", " << nextQ.v << endl;
return nextQ; // if it is avaible return the position, break the loop
}
}
}
nextQ.found = false; // No available position is found to return, found is set to false, return the position
return nextQ;
}
的其余部分的源代码,其中我有其他功能,例如产生禁止和isDisallowed和等是上this pastebin。我认为它不会真的与问题有关,这里的代码不应该太长。
第一组的结果如下所示: 那么我应该如何继续以便能够找到所有解决方案集?这是我卡住的地方。
你需要使用的代码,或将一个组合的解决方案就足够了? – abiessu
@abiessu我宁愿使用代码。有了这个组合解决方案,我甚至不需要走这么远,对吧?根据董事会的规模和皇后的数量,我可以计算出来。 – Erfan
为什么我对这个问题投下了票?有人可以向我解释吗? – Erfan