# N 皇后

<p><strong>n 皇后问题</strong> 研究的是如何将 <code>n</code> 个皇后放置在 <code>n×n</code> 的棋盘上，并且使皇后彼此之间不能相互攻击。</p><p>给你一个整数 <code>n</code> ，返回所有不同的 <strong>n<em> </em>皇后问题</strong> 的解决方案。</p><div class="original__bRMd"><div><p>每一种解法包含一个不同的 <strong>n 皇后问题</strong> 的棋子放置方案，该方案中 <code>'Q'</code> 和 <code>'.'</code> 分别代表了皇后和空位。</p><p> </p><p><strong>示例 1：</strong></p><img alt="" src="https://assets.leetcode.com/uploads/2020/11/13/queens.jpg" style="width: 600px; height: 268px;" /><pre><strong>输入：</strong>n = 4<strong><br />输出：</strong>[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]<strong><br />解释：</strong>如上图所示，4 皇后问题存在两个不同的解法。</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>n = 1<strong><br />输出：</strong>[["Q"]]</pre><p> </p><p><strong>提示：</strong></p><ul>	<li><code>1 <= n <= 9</code></li>	<li>皇后彼此不能相互攻击，也就是说：任何两个皇后都不能处于同一条横行、纵行或斜线上。</li></ul></div></div>

以下程序实现了这一功能，请你填补空白处内容：

```python
class Solution(object):
	def solveNQueens(self, n):
		if n == 0:
			return 0
		res = []
		board = [['.'] * n for t in range(n)]
		self.do_solveNQueens(res, board, n)
		return res
	def do_solveNQueens(self, res, board, num):
		if num == 0:
			res.append([''.join(t) for t in board])
			return
		ls = len(board)
		pos = ls - num
		check = [True] * ls
		for i in range(pos):
			for j in range(ls):
				if board[i][j] == 'Q':
					______________________;
		for j in range(ls):
			if check[j]:
				board[pos][j] = 'Q'
				self.do_solveNQueens(res, board, num - 1)
				board[pos][j] = '.'
if __name__ == '__main__':
	s = Solution()
	print (s.solveNQueens(4))
```

## template

```python
class Solution(object):
	def solveNQueens(self, n):
		if n == 0:
			return 0
		res = []
		board = [['.'] * n for t in range(n)]
		self.do_solveNQueens(res, board, n)
		return res
	def do_solveNQueens(self, res, board, num):
		if num == 0:
			res.append([''.join(t) for t in board])
			return
		ls = len(board)
		pos = ls - num
		check = [True] * ls
		for i in range(pos):
			for j in range(ls):
				if board[i][j] == 'Q':
					check[j] = False
					step = pos - i
					if j + step < ls:
						check[j + step] = False
					if j - step >= 0:
						check[j - step] = False
					break
		for j in range(ls):
			if check[j]:
				board[pos][j] = 'Q'
				self.do_solveNQueens(res, board, num - 1)
				board[pos][j] = '.'
if __name__ == '__main__':
	s = Solution()
	print (s.solveNQueens(4))
```

## 答案

```python
check[j] = False
step = pos - i
if j + step < ls:
	check[j + step] = False
if j - step >= 0:
	check[j - step] = False
break
```

## 选项

### A

```python
check[j] = False
step = pos - i
if j + step < ls:
	check[j + step] = False
if j - step >= 0:
	check[j - step] = False
```

### B

```python
check[j] = False
step = pos - i
if j + step < ls:
	check[j + step] = False
if j - step <= 0:
	check[j - step] = False
break
```

### C

```python
check[j] = False
step = pos - i
if j + step > ls:
	check[j + step] = False
if j - step <= 0:
	check[j - step] = False
break
```