[leetcode]N-Queens II

和I一样的。。。不过只输出可能数

感觉。。。可能有优化吧。。但是用I的代码过了。。。

class Solution {
public:
    bool col[100];
    bool d1[100]; // i + j
    bool d2[100]; // i - j + n
    int ans;    
    void nque( int n , int dep) {
        if(dep >= n) {
            ans ++;
            return ;
        }
        
        for(int i = 0 ; i < n ; i++) {
            if(col[i] && d1[dep+i] && d2[dep-i+n]) {
                col[i] = false;
                d1[dep+i] = false;
                d2[dep-i+n] = false;
                nque(n , dep + 1);
                col[i] = true;
                d1[dep+i] = true;
                d2[dep-i+n] = true;
            }
        }
    }
    int totalNQueens(int n) {
        memset(col , true , sizeof(col));
        memset(d1 , true , sizeof(d1));
        memset(d2 , true , sizeof(d2)); 
        ans = 0;
        nque(n , 0);
        return ans;
    }
};
原文地址:https://www.cnblogs.com/x1957/p/3515413.html