# N皇后 II

<div class="notranslate">
    <p><strong>n&nbsp;皇后问题</strong> 研究的是如何将 <code>n</code>&nbsp;个皇后放置在 <code>n×n</code> 的棋盘上，并且使皇后彼此之间不能相互攻击。</p>

    <p>给你一个整数 <code>n</code> ，返回 <strong>n 皇后问题</strong> 不同的解决方案的数量。</p>

    <p>&nbsp;</p>

    <div class="original__bRMd">
        <div>
            <p><strong>示例 1：</strong></p>
            <img style="width: 600px; height: 268px;" src="https://img-service.csdnimg.cn/img_convert/cc74a8b59b9a8fe64bb7a741774c2ad1.jpeg"
                alt="">
            <pre><strong>输入：</strong>n = 4
<strong>输出：</strong>2
<strong>解释：</strong>如上图所示，4 皇后问题存在两个不同的解法。
    </pre>

            <p><strong>示例 2：</strong></p>

            <pre><strong>输入：</strong>n = 1
<strong>输出：</strong>1
    </pre>

            <p>&nbsp;</p>

            <p><strong>提示：</strong></p>

            <ul>
                <li><code>1 &lt;= n &lt;= 9</code></li>
                <li>皇后彼此不能相互攻击，也就是说：任何两个皇后都不能处于同一条横行、纵行或斜线上。</li>
            </ul>
        </div>
    </div>
</div>

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

```java
class Solution {
	private boolean col[];
	private boolean dia1[];
	private boolean dia2[];
	public int totalNQueens(int n) {
		col = new boolean[n];
		dia1 = new boolean[2 * n - 1];
		dia2 = new boolean[2 * n - 1];
		return putQueen(n, 0);
	}
	private int putQueen(int n, int index) {
		int res = 0;
		if (index == n) {
			return 1;
		}
		for (int i = 0; i < n; i++) {
			if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {
				________________________;
			}
		}
		return res;
	}
}
```

## template

```java
class Solution {
	private boolean col[];
	private boolean dia1[];
	private boolean dia2[];
	public int totalNQueens(int n) {
		col = new boolean[n];
		dia1 = new boolean[2 * n - 1];
		dia2 = new boolean[2 * n - 1];
		return putQueen(n, 0);
	}
	private int putQueen(int n, int index) {
		int res = 0;
		if (index == n) {
			return 1;
		}
		for (int i = 0; i < n; i++) {
			if (!col[i] && !dia1[i - index + n - 1] && !dia2[i + index]) {
				col[i] = true;
				dia1[i - index + n - 1] = true;
				dia2[i + index] = true;
				res += putQueen(n, index + 1);
				col[i] = false;
				dia1[i - index + n - 1] = false;
				dia2[i + index] = false;
			}
		}
		return res;
	}
}
```

## 答案

```java
col[i] = true;
dia1[i - index + n - 1] = true;
dia2[i + index] = true;
res += putQueen(n, index + 1);
col[i] = false;
dia1[i - index + n - 1] = false;
dia2[i + index] = false;
```

## 选项

### A

```java
col[i] = false;
dia1[i - index + n - 1] = false;
dia2[i + index] = false;
res += putQueen(n, index + 1);
col[i] = true;
dia1[i - index + n - 1] = true;
dia2[i + index] = true;
```

### B

```java
col[i] = false;
dia1[i - index + n - 1] = false;
dia2[i + index] = false;
res += putQueen(n, index);
col[i] = true;
dia1[i - index + n - 1] = true;
dia2[i + index] = true;
```

### C

```java
col[i] = true;
dia1[i - index + n - 1] = true;
dia2[i + index] = true;
res += putQueen(n, index);
col[i] = false;
dia1[i - index + n - 1] = false;
dia2[i + index] = false;
```