52. N皇后 II/C++

51. N皇后/C++大同小异,甚至更简单些。

class Solution {
private:
    vector<bool> col,diaLeft,diaRight;
    
    int putQueen(int n,int index){
        int count=0;
        if(index==n)
            return 1;
        
        for(int i=0;i<n;++i){
            if(!col[i] && !diaLeft[index+i] && !diaRight[index-i+n-1]){
                col[i]=true;
                diaLeft[index+i]=true;
                diaRight[index-i+n-1]=true;
                
                count+=putQueen(n,index+1);
                
                col[i]=false;
                diaLeft[index+i]=false;
                diaRight[index-i+n-1]=false;
            }
        }
        return count;
    }
public:
    int totalNQueens(int n) {
        col=vector<bool>(n,false);
        diaLeft=vector<bool>(n,false);
        diaRight=vector<bool>(n,false);
        
        return putQueen(n,0);
    }
};

相关文章:

  • 2022-12-23
  • 2022-03-04
  • 2022-01-08
  • 2022-01-22
  • 2021-11-10
  • 2022-12-23
  • 2022-12-23
  • 2021-07-18
猜你喜欢
  • 2021-10-25
  • 2021-04-23
  • 2021-08-13
  • 2021-07-27
  • 2022-12-23
  • 2021-07-14
  • 2021-07-08
相关资源
相似解决方案