Skip to content

Commit f240b8b

Browse files
authored
Create 8Queens2.js
1 parent 1831d63 commit f240b8b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

8Queens2.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function queen(arr, row) {
2+
if (row>=arr.length) {
3+
return console.log(arr);
4+
}
5+
for (let col=0; col<arr.length; col++) {
6+
if (!isConflict(arr, row, col)) {
7+
arr[row] = col;
8+
queen(arr, row+1);
9+
}
10+
}
11+
}
12+
13+
function isConflict(arr, row, col) {
14+
for (let i=row-1; i>=0; i--) {
15+
if (col == arr[i] || (row-i) == Math.abs(col-arr[i]))
16+
return true;
17+
}
18+
return false;
19+
}
20+
21+
queen([0,0,0,0,0,0,0,0], 0);

0 commit comments

Comments
 (0)