#34 N-Queens II

原创 2016年08月29日 10:09:56

题目描述:

Follow up for N-Queens problem.

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

Example

For n=4, there are 2 distinct solutions.

题目思路:

这题和#33 差不多,把最后的ans改成count就好了。

Mycode(AC = 171ms):

class Solution {
public:
    /**
     * Calculate the total number of distinct N-Queen solutions.
     * @param n: The number of queens.
     * @return: The total number of distinct solutions.
     */
    int totalNQueens(int n) {
        // write your code here
        int count = 0;
        if (n == 0) return count;
        
        vector<string> sofar;
        unordered_set<int> col, diag;
        solveNQueens(count, sofar, n, col, diag);
        return count;
    }
    
    void solveNQueens(int& count,
                      vector<string>& sofar,
                      int n,
                      unordered_set<int>& col,
                      unordered_set<int>& diag)
    {
        if (sofar.size() == n) {
            count++;
            return;
        }
        
        string tmp = nDots(n);
        for (int i = 0; i < n; i++) {
            // if there is no Q in same row, col, and diag,
            // then tmp could be a potential ans
            if (col.find(i) == col.end() &&
                !findDiag(diag, sofar.size(), i, n))
            {
                col.insert(i);
                diag.insert(sofar.size() * n + i);
                tmp[i] = 'Q';
                sofar.push_back(tmp);
                
                solveNQueens(count, sofar, n, col, diag);
                
                sofar.pop_back();
                tmp[i] = '.';
                diag.erase(sofar.size() * n + i);
                col.erase(i);
            }
        }
    }
    
    // write n '.' in a line
    string nDots(int n) {
        string str = "";
        for (int i = 0; i < n; i++) {
            str += ".";
        }
        return str;
    }
    
    // check whether there is Q in diag
    bool findDiag(unordered_set<int>& diag, int i, int j, int n) {
        for (auto it = diag.begin(); it != diag.end(); it++) {
            int row = *it / n;
            int col = *it % n;
            
            if (abs(row - i) == abs(col - j)) return true;
        }
        
        return false;
    }
};


版权声明:三千微尘里,吾宁爱与憎

LeetCode52——N-Queens II

LeetCode52——N-Queens II 跟LeetCode51一样,只不过这题是求解解的个数。 这里我套用了上一题的代码,只不过当row==n(递归返回条件)时,不是将一种可行解加入结果集...

[leetcode] 52.N-Queens II

题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total num...

N-Queens II (Java)

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numb...

LeetCode 52. N-Queens II 解题报告

LeetCode 52. N-Queens II 解题报告

LeetCode | 52.N-Queens II

类似上一篇博客N皇后,但是由于这里的N会特别大(10^9),所以不能直接用数组,内存会爆掉.可以用动态数组vector每次push_back. 3ms AC.class Solution { pub...

LeetCode 51 N-Queens II

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe...

LeetCode 52. N-Queens II 题解(C++)

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numb...

【LeetCode从零单刷】N-Queens II

菜鸡从零单刷 LeetCode 系列!

LeetCode 52. N-Queens II(N皇后)

原题网址:https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead...
  • jmspan
  • jmspan
  • 2016年05月21日 06:27
  • 239

LeetCode_N-Queens II

LeetCode_N-Queens II
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:#34 N-Queens II
举报原因:
原因补充:

(最多只允许输入30个字)