bool check(vector<int> res) {
for (int row = 0; row < res.size()-1; row++) {
for (int col = row+1; col < res.size(); col++) {
if (row - col == res[row] - res[col] || row - col == res[col] - res[row])
return false;
}
}
return true;
}
void permQueen(vector<int> colIndex, int start, vector<vector<int> >& res) {
if (start==colIndex.size())
res.push_back(colIndex);
for (int i = start; i < colIndex.size(); i++) {
swap(colIndex[start], colIndex[i]);
permQueen(colIndex, start+1, res);
swap(colIndex[start], colIndex[i]);
}
}
int EightQueen() {
//const int queens = 8;
vector<int> colIndex = {0,1,2,3,4,5,6,7};
vector<vector<int> > res;
permQueen(colIndex, 0, res);
int cnt = 0;
for (int i = 0; i < res.size(); i++) {
if (check(res[i]))
cnt++;
}
return cnt;
}
8皇后问题
最新推荐文章于 2017-05-22 21:00:49 发布