8皇后问题

来源:互联网 发布:python修改文件内容 编辑:程序博客网 时间:2024/06/11 07:55
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;}
0 0
原创粉丝点击