Description
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return the number of distinct solutions to the n-queens puzzle.
Example:
1 | Input: 4 |
解法
与N皇后问题1基本一样,采用回溯法解题,只是在搜寻到一种情况之后对计数器做自加操作记录解的个数,这里就不重复描述啦。
具体代码如下:
1 | class Solution { |