• [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 };
  • 相关阅读:
    PHP笔记
    HTML5储存
    KeyDown,KeyPress和KeyUp详解(转)
    Vue.js和angular.js区别
    java 解析json的问题
    在Eclipse中使用JUnit4进行单元测试
    Ibatis代码自动生成工具——Abator安装与应用实例(图解)
    IT人员----怎么把电脑窗口设置成淡绿色
    Java面试题之数据库三范式是什么
    Java面试题之jsp相关
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4614716.html
Copyright © 2020-2023  润新知