<p>The <strong>n-queens</strong> puzzle is the problem of placing <code>n</code> queens on an <code>n x n</code> chessboard such that no two queens attack each other.</p>

<p>Given an integer <code>n</code>, return <em>all distinct solutions to the <strong>n-queens puzzle</strong></em>. You may return the answer in <strong>any order</strong>.</p>

<p>Each solution contains a distinct board configuration of the n-queens&#39; placement, where <code>&#39;Q&#39;</code> and <code>&#39;.&#39;</code> both indicate a queen and an empty space, respectively.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" />
<pre>
<strong>Input:</strong> n = 4
<strong>Output:</strong> [[&quot;.Q..&quot;,&quot;...Q&quot;,&quot;Q...&quot;,&quot;..Q.&quot;],[&quot;..Q.&quot;,&quot;Q...&quot;,&quot;...Q&quot;,&quot;.Q..&quot;]]
<strong>Explanation:</strong> There exist two distinct solutions to the 4-queens puzzle as shown above
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> [[&quot;Q&quot;]]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 9</code></li>
</ul>

class Solution {
public:
    vector<vector<string>> ans;
    map<string, int> _map;

    vector<string> posToStr(vector<vector<int>> &pos, int n) {
        int i, j;
        vector<string> ans;
        vector<vector<char>> matrix(n, vector<char>(n, '.'));
        for (i = 0; i < pos.size(); i++) {
            matrix[pos[i][0]][pos[i][1]] = 'Q';
        }

        for (int i = 0; i < n; i++) {
            string a;
            for (int j = 0; j < n; j++) {
                a += matrix[i][j];
            }
            ans.push_back(a);
        }
        return ans;
    }

    void fill(int x, int y, int n, vector<vector<int>> &mask) {
        int i, j;
        for (i = 0; i < n; i++) {
            mask[i][y] = 1;
        }
        for (i = 0; i < n; i++) {
            mask[x][i] = 1;
        }
        for (i = x, j = y; i >= 0 && j < n; i--, j++) mask[i][j] = 1;
        for (i = x, j = y; i < n && j >= 0; i++, j--) mask[i][j] = 1;
        for (i = x, j = y; i < n && j < n; i++, j++) mask[i][j] = 1;
    }

    void bt(vector<vector<int>> mask, vector<vector<int>> pos, int x, int y, int id, int n) {
        pos[id][0] = x;
        pos[id][1] = y;

        if (id == n - 1) {
            ans.push_back(posToStr(pos, n));
            return;
        }

        for (int i = 0; i < n; i++) {
            int j = id + 1;
            if (mask[i][j] == 0) {
                vector<vector<int>> new_mask = mask;
                fill(i, j, n, new_mask);
                bt(new_mask, pos, i, j, id + 1, n);
            }
        }
    }

    vector<vector<string>> solveNQueens(int n) {
        if (n == 1) {
            return {{"Q"}};
        }
        vector<vector<int>> position(n, vector<int>(2, 0));

        for (int i = 0; i < n; i++) {
            vector<vector<int>> masked(n, vector<int>(n, 0));
            fill(i, 0, n, masked);
            bt(masked, position, i, 0, 0, n);
        }
        return ans;
    }
};
43271