-
Notifications
You must be signed in to change notification settings - Fork 8
/
n-queens-ii.js
59 lines (56 loc) · 1.26 KB
/
n-queens-ii.js
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
// 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 the number of distinct solutions to the n-queens puzzle.
//
// Example:
//
//
// Input: 4
// Output: 2
// Explanation: There are two distinct solutions to the 4-queens puzzle as shown below.
// [
// [".Q..", // Solution 1
// "...Q",
// "Q...",
// "..Q."],
//
// ["..Q.", // Solution 2
// "Q...",
// "...Q",
// ".Q.."]
// ]
//
//
/**
* @param {number} n
* @return {number}
*/
var totalNQueens = function(n) {
var result = 0;
var current = new Array(n);
var isOk = function(deep, pos) {
for (var i = 0;i < deep;++i) {
if (current[i] === pos ||
Math.abs(deep - i) === Math.abs(pos - current[i])) {
return false;
}
}
return true;
};
var place = function(deep) {
if (deep === n) {
++result;
return ;
}
for (var i = 0;i < n;++i) {
if (isOk(deep, i)) {
current[deep] = i;
place(deep + 1);
}
}
};
place(0);
return result;
};