-
Notifications
You must be signed in to change notification settings - Fork 0
/
n-queens.js
53 lines (46 loc) · 1.13 KB
/
n-queens.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
/**
* @param {number} n
* @return {string[][]}
*/
var solveNQueens = function(n) {
var res = [];
if (n === 1 || n >= 4)
dfs(res, [], n, 0);
return res;
};
var dfs = function (res, points, n, index) {
for (var i = index; i < n; i++) {
if (points.length !== i)
return;
for (var j = 0; j < n; j++) {
if (isValid(points, [i, j])) {
points.push([i, j]);
dfs(res, points, n, i + 1);
if (points.length === n)
res.push(buildRes(points));
points.pop();
}
}
}
};
var buildRes = function (points) {
var res = [];
var n = points.length;
for (var i = 0; i < n; i++) {
res[i] = '';
for (var j = 0; j < n; j++) {
res[i] += (points[i][1] === j ? 'Q' : '.');
}
}
return res;
};
var isValid = function (oldPoints, newPoint) {
var len = oldPoints.length;
for (var i = 0; i < len; i++) {
if (oldPoints[i][0] === newPoint[0] || oldPoints[i][1] === newPoint[1])
return false;
if (Math.abs((oldPoints[i][0] - newPoint[0]) / (oldPoints[i][1] - newPoint[1])) === 1)
return false;
}
return true;
};