代码如下:
#include <iostream>
//using namespace std;
#define QUEEN_NUM 8
int ResultCounter = 0;
void QueenSolution(int *colume, int col);
bool check(int *colume, int col);
int main(void){
//数组colume中存放的是行值
//即假设col[0]==3,表明第1列中皇后在第4行上
int colume[QUEEN_NUM] = {0};
QueenSolution(colume, 0);
std::cout << "Solution Total Count: " << ResultCounter << std::endl;
}
void QueenSolution(int *colume, int col){
if(col == QUEEN_NUM){
ResultCounter++;
return;
}
for(int i = 0; i < QUEEN_NUM; i++){
colume[col] = i;
if(check(colume, col))
QueenSolution(colume, col + 1);
}
}
bool check(int *colume, int col){
for(int i = 0; i < col; i++){
if(colume[i] == colume[col] || colume[i] + i == colume[col] + col || colume[i] - i == colume[col] - col )
return false;
}
return true;
}