[LeetCode] N-Queens II

简介: If you have solved the N-Queens problem, this one can be solved in a similar manner. Starting from the first row, we try each of its columns.

If you have solved the N-Queens problem, this one can be solved in a similar manner. Starting from the first row, we try each of its columns. If there is no attack, we move on to the next row based previous rows. Otherwise, we backtrack to the current row and try another selection of column position. Once we meet the last row, increase the counts by 1.

The code is as follows.

 1 class Solution {
 2 public:
 3     int totalNQueens(int n) {
 4         int* colPos = new int [n];
 5         int counts = 0;
 6         solve(colPos, n, 0, counts);
 7         delete colPos;
 8         return counts;
 9     }
10 private:
11     bool noAttack(int*& colPos, int row, int col) {
12         for (int r = row - 1, ld = col - 1, rd = col + 1; r >= 0; r--, ld--, rd++)
13             if (colPos[r] == col || colPos[r] == ld || colPos[r] == rd)
14                 return false;
15         return true;
16     }
17     void solve(int*& colPos, int n, int row, int& counts) {
18         if (row == n) {
19             counts++;
20             return;
21         }
22         for (int col = 0; col < n; col++) {
23             colPos[row] = col;
24             if (noAttack(colPos, row, col))
25                 solve(colPos, n, row + 1, counts);
26         }
27     }
28 };

Someone has even suggested a damn clever solution to this problem using bit-manipulations in this link (refer to the first answer).

I rewrite the code below for reference.

 1 class Solution {
 2 public:
 3     int totalNQueens(int n) {
 4         int counts = 0;
 5         int limit = (1 << n) - 1;
 6         solve(0, 0, 0, limit, counts);
 7         return counts;
 8     }
 9 private:
10     void solve(int hProj, int lProj, int rProj, int limit, int& counts) {
11         if (hProj == limit) {
12             counts++;
13             return;
14         }
15         int pos = limit & (~(hProj | lProj | rProj));
16         while (pos) {
17             int p = pos & (-pos);
18             pos ^= p;
19             solve(hProj | p, (lProj | p) << 1, (rProj | p) >> 1, limit, counts);
20         }
21     }
22 };

The above two are both recursive solutions. This problem can also be solved iteratively as in this link.

I have also rewritten the code below for reference.

 1 class Solution {
 2 public:
 3     int totalNQueens(int n) {
 4         int* colPos = new int [n];
 5         memset(colPos, -1, n * sizeof(int));
 6         int row = 0, counts = 0;
 7         while (row >= 0) {
 8             if (row == n) {
 9                 counts++;
10                 row--;
11             }
12             int col = (colPos[row] == -1) ? 0 : colPos[row] + 1;
13             for (; col < n; col++) {
14                 if (noAttack(colPos, row, col)) {
15                     colPos[row] = col;
16                     row++;
17                     break;
18                 }
19             }
20             if (col == n) {
21                 colPos[row] = -1;
22                 row--;
23             }
24         }
25         return counts;
26     }
27 private:
28     bool noAttack(int*& colPos, int row, int col) {
29         for (int r = row - 1, ld = col - 1, rd = col + 1; r >= 0; r--, ld--, rd++)
30             if (colPos[r] == col || colPos[r] == ld || colPos[r] == rd)
31                 return false;
32         return true;
33     }
34 };

 

目录
相关文章
|
机器学习/深度学习
LeetCode 51. N-Queens
n-queens难题是将n个皇后放置在n×n棋盘上的问题,使得没有两个皇后互相攻击。 给定整数n,返回n-queens拼图的所有不同解。
96 0
LeetCode 51. N-Queens
LeetCode - 51. N-Queens
51. N-Queens  Problem's Link  ---------------------------------------------------------------------------- Mean:  N-Queen问题.
872 0
LeetCode - 52. N-Queens II
52. N-Queens II  Problem's Link  ---------------------------------------------------------------------------- Mean:  略.
803 0
[LeetCode] N-Queens
The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.
761 0
|
3月前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行