Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
103 lines (101 loc) · 2.94 KB

File metadata and controls

103 lines (101 loc) · 2.94 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
Author: King, nkuwjg@gmail.com
Date: Aug 23, 2013
Problem: N-Queens II
Difficulty: Medium
Source: https://oj.leetcode.com/problems/n-queens-ii/
Notes:
The n-queens puzzle is the problem of placing n queens on an n*n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q' and '.' both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
Solution: 1. Recursion.
2. Recursion + bit version. (fast)
The idea is from http://www.matrix67.com/blog/archives/266 (in chinese).
3. Iteration.
*/
public class Solution {
public int totalNQueens(int n) {
return totalNQueens_3(n);
}
public int totalNQueens_1(int n) {
int[] board = new int[n];
Arrays.fill(board,-1);
int[] res = new int[1];
totalNQueensRe(n, 0, board, res);
return res[0];
}
public void totalNQueensRe(int n, int row, int[] board, int[] res) {
if (n == row) {
res[0]++;
return;
}
for (int i = 0; i < n; ++i) {
if (isValid(board, row, i)) {
board[row] = i;
totalNQueensRe(n, row + 1, board, res);
board[row] = -1;
}
}
}
public boolean isValid(int[] board, int row, int col) {
for (int i = 0; i < row; ++i) {
if (board[i] == col || row - i == Math.abs(col - board[i]))
return false;
}
return true;
}
public int totalNQueens_2(int n) {
int[] res = new int[1];
totalNQueensRe2(n, 0, 0, 0, res);
return res[0];
}
public void totalNQueensRe2(int n, int row, int ld, int rd, int[] res) {
if (row == (1<<n) -1 ) {
res[0]++;
return;
}
int avail = ~(row | ld | rd);
for (int i = n - 1; i >= 0; --i) {
int pos = 1<<i;
if ((int)(avail&pos) != 0) {
totalNQueensRe2(n, row | pos, (ld|pos) << 1, (rd|pos) >>1, res);
}
}
}
public int totalNQueens_3(int n) {
int[] a = new int[n];
Arrays.fill(a,-1);
int res = 0;
int row = 0;
while (row >= 0) {
if (row == n) {
res++; row--;
}
int i = a[row] == -1 ? 0 : a[row] + 1;
for ( ; i < n; ++i) {
if (isValid(a, row, i)) {
a[row] = i;
row++;
break;
}
}
if (i == n) {
a[row] = -1;
row--;
}
}
return res;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.