**一. 題目描述**
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