C++ short code (8ms)


  • 3
    J
    vector<vector<string> > solveNQueens(int n) {
        vector<vector<string>> result; vector<int> r (n, -1);
        solve(result, r, 0);
        return result;
    }
    
    void solve(vector<vector<string> >& result, vector<int>& r, int curRow) {
        for(int xPos = 0; xPos<r.size(); xPos++) {
            r[curRow] = xPos;
            bool isSafe = true;
            for(int i = 0; i<curRow; i++) {
                if(r[i] == xPos || abs(curRow - i) == abs(xPos - r[i])) {
                    isSafe = false;
                    break;
                }
            }
            
            if(isSafe && curRow == r.size() - 1) {
                vector<string> v (r.size(), string(r.size(), '.'));
                for(int i = 0; i<r.size(); i++) 
                    v[i][r[i]] = 'Q';
                result.push_back(v);
            }
            else if(isSafe) {
                solve(result, r, curRow + 1);
            }
        }
    }

Log in to reply
 

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