forked from ecmadao/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo52.n-queens-II.js
More file actions
54 lines (50 loc) · 1.41 KB
/
No52.n-queens-II.js
File metadata and controls
54 lines (50 loc) · 1.41 KB
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
/**
* Difficulty:
* Hard
*
* Desc:
* Follow up for N-Queens problem.
* Now, instead outputting board configurations, return the total number of distinct solutions
*
* 还是 n 皇后问题,只是相对于上一题,这次只需要求出解的数目即可
*/
var getQueues = function(n) {
var results = 0;
var getQueue = function(row, notAvaliableColumn, finishedRowCount) {
if (row > n) return true;
var result = false;
for (var i = 0; i < n; i += 1) {
if (notAvaliableColumn.has(`c${i}`) || notAvaliableColumn.has(`s${row + i}`) || notAvaliableColumn.has(`d${row - i}`)) continue;
var notAvaliable = new Set([...notAvaliableColumn]);
notAvaliable.add(`c${i}`);
notAvaliable.add(`d${row - i}`);
notAvaliable.add(`s${row + i}`);
var qRow = new Array(n).fill('.');
qRow[i] = 'Q';
if (getQueue(row + 1, notAvaliable, finishedRowCount + 1)) {
if (finishedRowCount >= n) {
result = true;
results += 1;
break;
}
} else {
notAvaliable.delete(`c${i}`);
notAvaliable.delete(`d${row - i}`);
notAvaliable.delete(`s${row + i}`);
}
}
return result;
};
getQueue(1, new Set(), 1);
return results;
};
/**
* @param {number} n
* @return {number}
*/
var totalNQueens = function(n) {
if (n === 1) return 1;
if (n <= 3) return 0;
var results = getQueues(n);
return results;
};