-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path52_javascript.js
More file actions
52 lines (43 loc) · 1.07 KB
/
52_javascript.js
File metadata and controls
52 lines (43 loc) · 1.07 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
// n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
// 给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量。
//
// 示例 1:
// 输入:n = 4
// 输出:2
// 解释:如上图所示,4 皇后问题存在两个不同的解法。
// 示例 2:
// 输入:n = 1
// 输出:1
//
// 提示:
// 1 <= n <= 9
// 皇后彼此不能相互攻击,也就是说:任何两个皇后都不能处于同一条横行、纵行或斜线上。
/**
* @param {number} n
* @return {number}
*/
// 和 51题一样
var totalNQueens = function (n) {
let col = {};
let bias = {};
let bias2 = {};
let res = 0;
let dfs = i => {
if (i == n) {
res++;
}
for (let j = 0; j < n; j++) {
if (!col[j] && !bias[i + j] && !bias2[i - j]) {
col[j] = true;
bias[i + j] = true;
bias2[i - j] = true;
dfs(i + 1);
col[j] = false;
bias[i + j] = false;
bias2[i - j] = false;
}
}
};
dfs(0);
return res;
};