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

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

```python
class Solution(object):
	def __init__(self):
		self.count = 0
	def totalNQueens(self, n):
		self.dfs(0, n, 0, 0, 0)
		return self.count
	def dfs(self, row, n, column, diag, antiDiag):
		if row == n:
			self.count += 1
			return
		for index in range(n):
			isColSafe = (1 << index) & column == 0
			isDigSafe = (1 << (n - 1 + row - index)) & diag == 0
			isAntiDiagSafe = (1 << (row + index)) & antiDiag == 0
			if isAntiDiagSafe and isColSafe and isDigSafe:
				_________________________;
if __name__ == '__main__':
	s = Solution()
	print (s.totalNQueens(4))
```

## template

```python
class Solution(object):
	def __init__(self):
		self.count = 0
	def totalNQueens(self, n):
		self.dfs(0, n, 0, 0, 0)
		return self.count
	def dfs(self, row, n, column, diag, antiDiag):
		if row == n:
			self.count += 1
			return
		for index in range(n):
			isColSafe = (1 << index) & column == 0
			isDigSafe = (1 << (n - 1 + row - index)) & diag == 0
			isAntiDiagSafe = (1 << (row + index)) & antiDiag == 0
			if isAntiDiagSafe and isColSafe and isDigSafe:
				self.dfs(row + 1,  n, (1 << index) | column,
						 (1 << (n - 1 + row - index)) | diag,
						 (1 << (row + index)) | antiDiag)
if __name__ == '__main__':
	s = Solution()
	print (s.totalNQueens(4))
```

## 答案

```python
self.dfs(row + 1,  n, (1 << index) | column,
        (1 << (n - 1 + row - index)) | diag,
        (1 << (row + index)) | antiDiag)
```

## 选项

### A

```python
self.dfs(row + 1,  n, (index >> 1) | column,
        (1 << (n - 1 + row - index)) | diag,
        (1 << (row + index)) | antiDiag)
```

### B

```python
self.dfs(row + 1,  n, (index >> 1) | column,
        (1 << (n + 1 + row - index)) | diag,
        (1 << (row + index)) | antiDiag)
```

### C

```python
self.dfs(row + 1,  n, (1 << index) | column,
        (1 << (n + 1 + row - index)) | diag,
        (1 << (row + index)) | antiDiag)
```