leetcodeOJ 52. N-Queens II

来源:互联网 发布:seo效果检测步骤包括 编辑:程序博客网 时间:2024/06/01 10:23

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.

方法类似leetcodeOJ51题

class Solution {public:    int totalNQueens(int n) {        int ans = 0;        vector<string> re(n, string(n, '.'));        genNQueens(re, ans, 0, n);        return ans;    }    private:    void genNQueens(vector<string>& re, int &ans, int row, int n){        if(row == n){            ans++;            return;        }        for(int i = 0; i < n; i++){            if(isValid(re, row, i, n)){                re[row][i] = 'Q';                genNQueens(re, ans, row+1, n);                re[row][i] = '.';            }        }    }    bool isValid(vector<string>& re, int row, int col, int n){        for(int i = 0; i < row; i++){            if(re[i][col] == 'Q')                return false;        }        for(int i = row-1, j = col-1; i >= 0 && j >= 0; i--, j--){            if(re[i][j] == 'Q')                return false;        }        for(int i = row-1, j = col+1; i >= 0 && j < n; i--, j++){            if(re[i][j] == 'Q')                return false;        }                return true;    }    };

0 0
原创粉丝点击