leetcode N-Queens II

原创 2013年12月01日 22:53:23

N-Queens II

 Total Accepted: 1560 Total Submissions: 6035My Submissions

Follow up for N-Queens problem.

Now, instead outputting board configurations, return the total number of distinct solutions.


The code similar to I is TLE

class Solution {
 public:
  bool check(vector<vector<int>>& board, int x, int y) {
    int size = board.size(), xx, yy;
    for (int i = 0; i < x; ++i) 
      if (board[i][y])
        return false;
    for (int i = 0; i < size; ++i) 
      if (board[x][i])
        return false;
    for (xx = --x, yy = --y; xx >= 0 && yy >= 0; --xx, --yy) 
      if (board[xx][yy])
        return false;
    for (xx = --x, yy = ++y; xx >= 0 && yy < size; --xx, ++yy) 
      if (board[xx][yy])
        return false;
    return true;
  }
  void dfs(vector<vector<int>>& board, int& count, int start) {
    int size = board.size();
    if (start == board.size()) {
      ++count;
      return;
    }
    for (int i = 0; i < size; ++i) {
      if (check(board, start, i)) {
        board[start][i] = 1;
        dfs(board, count, start + 1);
        board[start][i] = 0;
      }
    }
  }
  int totalNQueens(int n) {
    if (n == 0)
      return 0;
    int res = 0;
    vector<vector<int>> board(n, vector<int>(n, 0));
    dfs(board, res, 0);
    return res;
  }
};
The main problem is that the check function wastes too much time. 

Two ways to avoid this:

1. Use an array to record the position row[i] , col[i](true, false)

2. BIt manipulation

class Solution {
 public:
  unsigned int upperlimit;
  void dfs(int& count, unsigned int row, unsigned int ld, unsigned int rd) {
    if (row == upperlimit)
      ++count;
    unsigned int pos = (row | ld | rd) & upperlimit; // the bit 1 simplify the position can't place the queue any more
    unsigned int p = (~pos) & upperlimit,digit = 0;
    while (p) {
      digit = p - (p & (p - 1)); // Find the rightest 1
      //digit = p&(~p + 1);
      dfs(count, row + digit, (ld + digit) >> 1, (rd + digit) << 1);
      p -= digit;
    }
  }
  int totalNQueens(int n) {
    upperlimit = 0;
    int count = 0;
    for (int i = 0; i < n; ++i)
      upperlimit |= (1<<i);
    dfs(count, 0, 0, 0);
    return count;
  }
};


This is a WA code, find the problem!!!!!!

class Solution {
 public:
  unsigned int upperlimit;
  int res = 0;
  void dfs(unsigned int row, unsigned int ld, unsigned int rd) {
    if (row == upperlimit)
      ++res;
    unsigned int pos = (row | ld | rd) & upperlimit, p = (~pos) & upperlimit, digit;
    while (p) {
      digit = p - p & (p - 1);
      dfs(row + digit, (ld + digit) << 1, (rd + digit) >> 1);
      p -= digit;
    }
  };
  int totalNQueens(int n) {
    for (int i = 0; i < n; ++i)
      upperlimit |= (1 << i);
    res = 0;
    dfs(0, 0, 0);
    return res;  
  }
};



 

相关文章推荐

LeetCode52——N-Queens II

LeetCode52——N-Queens II 跟LeetCode51一样,只不过这题是求解解的个数。 这里我套用了上一题的代码,只不过当row==n(递归返回条件)时,不是将一种可行解加入结果集...

[leetcode] 52.N-Queens II

题目:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total num...

LeetCode 52. N-Queens II 解题报告

LeetCode 52. N-Queens II 解题报告

LeetCode | 52.N-Queens II

类似上一篇博客N皇后,但是由于这里的N会特别大(10^9),所以不能直接用数组,内存会爆掉.可以用动态数组vector每次push_back. 3ms AC.class Solution { pub...

LeetCode 51 N-Queens II

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe...

LeetCode 52. N-Queens II 题解(C++)

Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numb...

【LeetCode从零单刷】N-Queens II

菜鸡从零单刷 LeetCode 系列!

LeetCode 52. N-Queens II(N皇后)

原题网址:https://leetcode.com/problems/n-queens-ii/ Follow up for N-Queens problem. Now, instead...
  • jmspan
  • jmspan
  • 2016年05月21日 06:27
  • 223

LeetCode_N-Queens II

LeetCode_N-Queens II

leetcode:N-Queens II 的位运算非递归解法

原 Follow up for N-Queens problem. Now, instead outputting board configurations, return the tot...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode N-Queens II
举报原因:
原因补充:

(最多只允许输入30个字)