zoukankan      html  css  js  c++  java
  • [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 };
  • 相关阅读:
    phpmyadmin 设置密码
    php 单向链表反转 reverse (没有空的头结点)
    vue 学习资料
    vue 熟悉项目结构 创建第一个自己的组件
    vue开发环境配置
    vue.js 配置axios 用来ajax请求数据
    javascript 中介者模式 mediator
    PHP 相对路径转换为绝对路径 realpath
    Typescript, ES6
    POST、GET、@RequestBody和@RequestParam区别
  • 原文地址:https://www.cnblogs.com/jcliBlogger/p/4614716.html
Copyright © 2011-2022 走看看