和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;
    }
};

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-06-29
  • 2021-07-10
  • 2022-01-06
  • 2021-07-21
  • 2021-07-21
  • 2021-05-30
猜你喜欢
  • 2021-06-27
  • 2021-10-17
  • 2021-09-16
  • 2021-08-27
  • 2021-12-25
相关资源
相似解决方案