<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 {
public:
  bool can_place(int row, int col, const vector<vector<bool>> &board) {
    // check row
    for (size_t col = 0; col < board.size(); col++) {
      if (board[row][col]) {
        return false;
      }
    }
    // check column
    for (size_t row = 0; row < board.size(); row++) {
      if (board[row][col]) {
        return false;
      }
    }
    // check diag1-left
    {
      int neg = 0;
      while (row - neg >= 0 && col - neg >= 0) {
        if (board[row - neg][col - neg]) {
          return false;
        }
        neg++;
      }
    }
    // check diag1-right
    {
      int pos = 0;
      while (row + pos < board.size() && col + pos < board.size()) {
        if (board[row + pos][col + pos]) {
          return false;
        }
        pos++;
      }
    }
    // check diag2-left
    {
      int inc = 0;
      while (row + inc < board.size() && col - inc >= 0) {
        if (board[row + inc][col - inc]) {
          return false;
        }
        inc++;
      }
    }
    // check diag2-right
    {
      int inc = 0;
      while (row - inc >= 0 && col + inc < board.size()) {
        if (board[row - inc][col + inc]) {
          return false;
        }
        inc++;
      }
    }
    return true;
  }
  void search(int &count, int &placed, vector<vector<bool>> &board,
              int start_row, int start_col) {
    if (placed >= board.size()) {
      count++;
      return;
    }
    size_t row = start_row;
    size_t col = start_col;
    while (row < board.size() && col < board.size()) {
      if (can_place((int)row, (int)col, board)) {
        board[row][col] = true;
        placed++;
        if (col + 1 < board.size()) {
          search(count, placed, board, row, col + 1);
        } else {
          search(count, placed, board, row + 1, 0);
        }
        placed--;
        board[row][col] = false;
      }
      col++;
      if (col == board.size()) {
        col = 0;
        row++;
      }
    }
  }
  int totalNQueens(int n) {
    vector<vector<bool>> board(n, vector(n, false));
    int count = 0;
    int placed = 0;
    search(count, placed, board, 0, 0);
    return count;
  }
};


9932