题意:
解题思路:
思路:1. 解法同:51. N皇后
PHP代码实现:
class Solution {  public $cols = [];  public $pie = [];  public $na = [];  public $res = [];  function totalNQueens($n) {    $this->solve([], $n, 0);    return count($this->res);  }  function solve($answer, $n, $i) {    if ($i == $n) {      array_push($this->res, $answer);      return;    }    for ($j = 0; $j < $n; $j++) {      if (in_array($j, $this->cols) || in_array($i + $j, $this->pie) || in_array($i - $j + $n, $this->na)) {        continue;      }      array_push($this->cols, $j);      array_push($this->pie, $i + $j);      array_push($this->na, $i - $j + $n);      $answer[$i] = $j;      $this->solve($answer, $n, $i + 1);      array_pop($this->cols);      array_pop($this->pie);      array_pop($this->na);    }  }}
GO代码实现:
func totalNQueens(n int) int {    res := make([][]int, 0)    cols := make(map[int]bool, n)    pie := make(map[int]bool, n)    na := make(map[int]bool, n)    dfs([]int{}, n, cols, pie, na, &res)    return len(res)}func dfs(rows []int, n int, cols, pie, na map[int]bool, res *[][]int) {    row := len(rows)    if row == n {        temp := make([]int, len(rows))        copy(temp, rows)        (*res) = append((*res), temp)        return    }    for col := 0; col < n; col++ {        if !cols[col] && !pie[row + col] && !na[row - col] {            cols[col], pie[row + col], na[row - col]  = true, true, true;            dfs(append(rows, col), n, cols, pie, na, res)            cols[col], pie[row + col], na[row - col]  = false, false, false;        }    }}