<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>the number of distinct solutions to the&nbsp;<strong>n-queens puzzle</strong></em>.</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> 2
<strong>Explanation:</strong> There are two distinct solutions to the 4-queens puzzle as shown.
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

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

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

class Solution:
    def totalNQueens(self, n: int) -> List[List[str]]:
        self.output = list()
        for i in range(n):
            # First row or col will have exactly one queen
            test = [["." for i in range(n)] for j in range(n)]
            test[0][i] = 'Q'
            self.recurse(1, test)
               
        return len(self.output)
    def listToStrings(self, boardState):
        output = [""] * len(boardState)
        for row in range(len(boardState)):
            for i in range(len(boardState)):
                output[row] += boardState[row][i]
        return output
    def recurse(self, row, boardState):
        if row >= len(boardState):
            self.output.append(self.listToStrings(boardState))
            return
        for i in range(len(boardState)):
            if self.testRecentQueen(row, i, boardState):
                boardState[row][i] = 'Q'
                self.recurse(row + 1, boardState)
                boardState[row][i] = '.'

    def testRecentQueen(self, row, col, boardState):
        for i in range(len(boardState)):
            if i != col and boardState[row][i] == 'Q':
                return False
            if i != row and boardState[i][col] == 'Q':
                return False

        for offset in range(1, len(boardState)):
            if row + offset < len(boardState):
                if col + offset < len(boardState):
                    if boardState[row+offset][col+offset] == 'Q':
                        return False
                if col - offset >= 0:
                    if boardState[row+offset][col - offset] == 'Q':
                        return False

            if row - offset >= 0:
                if col + offset < len(boardState):
                    if boardState[row-offset][col+offset] == 'Q':
                        return False
                if col - offset >= 0:
                    if boardState[row-offset][col - offset] == 'Q':
                        return False
        return True

    
17000