# 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://assets.leetcode.com/uploads/2020/11/13/queens.jpg"
                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>

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

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	int totalNQueens(int n)
	{
		vector<int> stack(n);
		return dfs(n, 0, stack);
	}
private:
	int dfs(int n, int row, vector<int> &stack)
	{
		int count = 0;
		if (row == n)
		{
			return count + 1;
		}
		else
		{
			for (int i = 0; i < n; i++)
			{
				if (row == 0 || !conflict(stack, row, i))
				{
					stack[row] = i;
					_______________________;
				}
			}
			return count;
		}
	}
	bool conflict(vector<int> &stack, int row, int col)
	{
		for (int i = 0; i < row; i++)
		{
			if (col == stack[i] || abs(row - i) == abs(col - stack[i]))
			{
				return true;
			}
		}
		return false;
	}
}
```

## template

```cpp
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
	int totalNQueens(int n)
	{
		vector<int> stack(n);
		return dfs(n, 0, stack);
	}
private:
	int dfs(int n, int row, vector<int> &stack)
	{
		int count = 0;
		if (row == n)
		{
			return count + 1;
		}
		else
		{
			for (int i = 0; i < n; i++)
			{
				if (row == 0 || !conflict(stack, row, i))
				{
					stack[row] = i;
					count += dfs(n, row + 1, stack);
				}
			}
			return count;
		}
	}
	bool conflict(vector<int> &stack, int row, int col)
	{
		for (int i = 0; i < row; i++)
		{
			if (col == stack[i] || abs(row - i) == abs(col - stack[i]))
			{
				return true;
			}
		}
		return false;
	}
}
```

## 答案

```cpp
count += dfs(n, row + 1, stack);
```

## 选项

### A

```cpp
count += dfs(n, row - 1, stack);
```

### B

```cpp
count += dfs(n, row, stack);
```

### C

```cpp
count += dfs(n + 1, row, stack);
```