3 ms dfs c++


  • 0
    P
    typedef vector<bool> vb;
    class Solution {
        vector<vb> dp;
        void whatTheFuck(int &ans,int k,int &n){
            if(k==n){
                ++ans;
                return;
            }
            for(int i=0;i<n;++i) if(dp[0][i] && dp[1][k+i] && dp[2][n+k-i]){
                dp[0][i] = dp[1][k+i] = dp[2][n+k-i] = false;
                whatTheFuck(ans,k+1,n);
                dp[0][i] = dp[1][k+i] = dp[2][n+k-i] = true;
            }
        }
    public:
        int totalNQueens(int n) {
            dp = vector<vb>(3,vb(2*n+1,true));
            int ans;
            whatTheFuck(ans,0,n);
            return ans;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.