【LeetCode】52. N Queen II

1. Title

The n queen problem studies how to place n queens on an n×n chessboard and make the queens unable to attack each other.
queens
The picture above shows a solution to the 8 Queens problem.

Given an integer n, return the number of different solutions for n queens.

Example:

输入: 4
输出: 2
解释: 4 皇后问题存在如下两个不同的解法。
[
 [".Q..",  // 解法 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // 解法 2
  "Q...",
  "...Q",
  ".Q.."]
]

prompt:

  • The queen is the pawn in chess and means the wife of the king. The queen only does one thing, and that is to "eat children". When she met a chess piece that could be eaten, she quickly rushed to take it. Of course, she can take one step or N-1 steps horizontally, vertically and diagonally, and can go forward or backward. (Quoted from Baidu Encyclopedia-Queen)

Two, solve

1. Return directly

Ideas:

Calculate the result directly with the existing program, then put it into the array, and return directly when needed. It's a bit tricky, but it can actually be combined with the second method to speed up access.

Code:

int totalNQueens(int n) {
    
    
    int ans[] = {
    
    1, 0, 0, 2, 10, 4, 40, 92, 352, 724, 2680, 14200, 73712, 365596, 2279184, 14772512, 95815104, 666090624};
    return ans[n - 1];
}

Time complexity: O(1)
Space complexity: O(n)

2、DFS

Ideas:

Scan line by line, backtracking of unqualified. For more details, please see: 【LeetCode】51. N Queen

Code:

class Solution {
    
    
    int cnt = 0;
    boolean[] cols;
    boolean[] diag1;
    boolean[] diag2;
    public int totalNQueens(int n) {
    
    
        cols = new boolean[n];
        diag1 = new boolean[2*n];
        diag2 = new boolean[2*n];
        DFS(0, n);
        return cnt;
    }

    private void DFS(int row, int n) {
    
    
        if (row==n) {
    
    
            cnt++;
        }
        for (int col=0; col<n; col++) {
    
    
            if (cols[col] || diag1[row-col+n] || diag2[row+col])  continue;
            cols[col]=true; diag1[row-col+n]=true; diag2[row+col]=true;
            DFS(row+1, n);
            cols[col]=false; diag1[row-col+n]=false; diag2[row+col]=false;
        }
    }
}

Time complexity: O(n!), where n is the number of queens.
Space complexity: O(n), the number of recursive call layers, up to n layers.

3. Bit operation

Ideas:

It is not easy to understand, put it aside for now.

Code:

class Solution {
    
    
  
  private int size;
  private int count;

  private void solve(int row, int ld, int rd) {
    
    
    if (row == size) {
    
    
      count++;
      return;
    }
    int pos = size & (~(row | ld | rd));
    while (pos != 0) {
    
    
      int p = pos & (-pos);
      pos -= p;
      solve(row + p, (ld + p) << 1, (rd + p) >> 1);
    }
  }
  
  public int totalNQueens(int n) {
    
    
    count = 0;
    size = (1 << n) - 1;
    solve(0, 0, 0);
    return count;  
  }
}

Time complexity: O (?) O(?)O ( ? )
Space complexity: O (?) O(?)The ( ? )

Three, reference

1、Accepted Java Solution
2、Easiest Java Solution (1ms, 98.22%)
3、Collection of solutions in Java
4、A nice hack (might be very informative specially for new users)
5、Java AC, bit M, over 98%, low space
6、JAVA - Clean Bitwise Implementation - Beats 100% - 0ms

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/108564488