zoukankan      html  css  js  c++  java
  • Leetcode#52 N-Queens II

    原题地址

    N-Queens几乎没差别,好像还更简单了呢

    代码:

     1 int solve(int row, vector<bool> &col, vector<bool> left, vector<bool> right) {
     2   if (row == 0)
     3     return 1;
     4 
     5   vector<bool> avail(col.size(), false);
     6   for (int i = 0; i < col.size(); i++)
     7     avail[i] = !(col[i] || left[i] || right[i]);
     8 
     9   for (int i = 0; i < col.size() - 1; i++)
    10     left[i] = left[i + 1];
    11   left[col.size() - 1] = false;
    12   for (int i = col.size() - 1; i > 0; i--)
    13     right[i] = right[i - 1];
    14   right[0] = false;
    15 
    16   int res = 0;
    17   for (int i = 0; i < col.size(); i++) {
    18     if (!avail[i])
    19       continue;
    20     col[i] = true;
    21     if (i > 0) left[i - 1] = true;
    22     if (i < col.size() - 1) right[i + 1] = true;
    23     res += solve(row - 1, col, left, right);
    24     if (i < col.size() - 1) right[i + 1] = false;
    25     if (i > 0) left[i - 1] = false;
    26     col[i] = false;
    27   }
    28 
    29   return res;
    30 }
    31 
    32 int totalNQueens(int n) {
    33   vector<bool> col = vector<bool>(n, false);
    34   vector<bool> left = vector<bool>(n, false);
    35   vector<bool> right = vector<bool>(n, false);
    36   return solve(n, col, left, right);
    37 }
  • 相关阅读:
    OilPaint(转载/实验)
    UE4 3D artist
    render pipeline about (翻译)
    Python 相对导入 碎碎念
    USF, USH Grammar
    JZ19 顺时针打印矩阵
    JZ49 把字符串转换成整数
    JZ45 扑克牌顺子
    JZ53 表示数值的字符串
    JZ48 不用加减乘除做加法
  • 原文地址:https://www.cnblogs.com/boring09/p/4245357.html
Copyright © 2011-2022 走看看