LeetCode(52):N皇后 II

Hard!

题目描述:

皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。

上图为 8 皇后问题的一种解法。

给定一个整数 n,返回 n 皇后不同的解决方案的数量。

示例:

输入: 4
输出: 2
解释: 4 皇后问题存在如下两个不同的解法。
[
 [".Q..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]

解题思路:

这道题是之前那道 N-Queens N皇后问题 的延伸,说是延伸其实我觉得两者顺序应该颠倒一下,上一道题比这道题还要稍稍复杂一些,二者本质上没有什么区别,都是要用回溯法Backtracking来解,如果理解了之前那道题的思路,此题只要做很小的改动即可,不再需要求出具体的皇后的摆法,只需要每次生成一种解法时,计数器加一即可。

C++解法一:

 1 class Solution {
 2 public:
 3     int totalNQueens(int n) {
 4         int res = 0;
 5         vector<int> pos(n, -1);
 6         totalNQueensDFS(pos, 0, res);
 7         return res;
 8     }
 9     void totalNQueensDFS(vector<int> &pos, int row, int &res) {
10         int n = pos.size();
11         if (row == n) ++res;
12         else {
13             for (int col = 0; col < n; ++col) {
14                 if (isValid(pos, row, col)) {
15                     pos[row] = col;
16                     totalNQueensDFS(pos, row + 1, res);
17                     pos[row] = -1;
18                 }
19             }
20         }
21     }
22     bool isValid(vector<int> &pos, int row, int col) {
23         for (int i = 0; i < row; ++i) {
24             if (col == pos[i] || abs(row - i) == abs(col - pos[i])) {
25                 return false;
26             }
27         }
28         return true;
29     }
30 };

C++解法二:

 1 class Solution {
 2 private:
 3     int res;
 4 public:
 5     int totalNQueens(int n) {
 6         vector<int> state(n, -1);
 7         res = 0;
 8         helper(state, 0);
 9         return res;
10     }
11     void helper(vector<int> &state, int row)
12     {//放置第row行的皇后
13         int n = state.size();
14         if(row == n)
15         {
16             res++;
17             return;
18         }
19         for(int col = 0; col < n; col++)
20             if(isValid(state, row, col))
21             {
22                 state[row] = col;
23                 helper(state, row+1);
24                 state[row] = -1;;
25             }
26     }
27      
28     //判断在row行col列位置放一个皇后,是否是合法的状态
29     //已经保证了每行一个皇后,只需要判断列是否合法以及对角线是否合法。
30     bool isValid(vector<int> &state, int row, int col)
31     {
32         for(int i = 0; i < row; i++)//只需要判断row前面的行,因为后面的行还没有放置
33             if(state[i] == col || abs(row - i) == abs(col - state[i]))
34                 return false;
35         return true;
36     }
37  
38 };

猜你喜欢

转载自www.cnblogs.com/ariel-dreamland/p/9143370.html
今日推荐