# 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>

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

```java
import java.util.List;
import java.util.ArrayList;
public class Solution {
	public List<List<String>> solveNQueens(int n) {
		List<List<String>> res = new ArrayList<List<String>>();
		int[] queenList = new int[n];
		placeQueen(queenList, 0, n, res);
		return res;
	}
	private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {
		if (row == n) {
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 0; i < n; i++) {
				String str = "";
				for (int col = 0; col < n; col++) {
					if (queenList[i] == col) {
						str += "Q";
					} else {
						str += ".";
					}
				}
				list.add(str);
			}
			res.add(list);
		}
		for (int col = 0; col < n; col++) {
			if (isValid(queenList, row, col)) {
				queenList[row] = col;
				________________________;
			}
		}
	}
	private boolean isValid(int[] queenList, int row, int col) {
		for (int i = 0; i < row; i++) {
			int pos = queenList[i];
			if (pos == col) {
				return false;
			}
			if (pos + row - i == col) {
				return false;
			}
			if (pos - row + i == col) {
				return false;
			}
		}
		return true;
	}
}
```

## template

```java
import java.util.List;
import java.util.ArrayList;
public class Solution {
	public List<List<String>> solveNQueens(int n) {
		List<List<String>> res = new ArrayList<List<String>>();
		int[] queenList = new int[n];
		placeQueen(queenList, 0, n, res);
		return res;
	}
	private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {
		if (row == n) {
			ArrayList<String> list = new ArrayList<String>();
			for (int i = 0; i < n; i++) {
				String str = "";
				for (int col = 0; col < n; col++) {
					if (queenList[i] == col) {
						str += "Q";
					} else {
						str += ".";
					}
				}
				list.add(str);
			}
			res.add(list);
		}
		for (int col = 0; col < n; col++) {
			if (isValid(queenList, row, col)) {
				queenList[row] = col;
				placeQueen(queenList, row + 1, n, res);
			}
		}
	}
	private boolean isValid(int[] queenList, int row, int col) {
		for (int i = 0; i < row; i++) {
			int pos = queenList[i];
			if (pos == col) {
				return false;
			}
			if (pos + row - i == col) {
				return false;
			}
			if (pos - row + i == col) {
				return false;
			}
		}
		return true;
	}
}
```

## 答案

```java
placeQueen(queenList, row + 1, n, res);
```

## 选项

### A

```java
placeQueen(queenList, row, n, res);
```

### B

```java
placeQueen(queenList, row + 1, n + 1, res);
```

### C

```java
placeQueen(queenList, row, n + 1, res);
```