**一. 题目描述**
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
**二. 题目分析**
题目与N-Queens的方法基本相同,但比后者更为简单,因为题目只要求输出合适解的个数,不需要打印所有解,代码要比上一题简化很多。设一个全局变量,每遍历到底部一次就增1。
**三. 示例代码**
~~~
#include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
int totalNQueens(int n)
{
this->row = vector<int>(n, 0); // 行信息
this->col = vector<int>(n, 0); // 列信息
result = 0;
dfs(0, n, result); // 深搜
return result;
}
private:
int result; // 存放打印的结果
vector<int> row; // 记录每一行哪个下标是Q
vector<int> col; // 记录每一列是否已有Q
void dfs(int r, int n, int & result) // 遍历第r行,棋盘总共有n行
{
if (r == n) // 可遍历到棋盘底部,计数器加1
++result;
int i, j;
for (i = 0; i < n; ++i)
{
if (col[i] == 0)
{
for (j = 0; j < r; ++j)
if (abs(r - j) == abs(row[j] - i))
break;
if (j == r)
{
col[i] = 1; // 标记第i列,已存在Q
row[j] = i; // 第j行的第i个元素放入Q
dfs(r + 1, n, result); // 遍历第r + 1行
col[i] = 0;
row[j] = 0;
}
}
}
}
};
~~~
**四. 小结**
N-Queens的后续题目N-Queens II,比前一题简化许多,因为只要求输出解的个数,不需要输出所有解的具体状况。
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching