[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 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 };

 

转载于:https://www.cnblogs.com/jcliBlogger/p/4614716.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值