Skip to content

Commit

Permalink
Create 8Queens2.js
Browse files Browse the repository at this point in the history
  • Loading branch information
deathnopool authored Nov 19, 2019
1 parent 1831d63 commit f240b8b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions 8Queens2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function queen(arr, row) {
if (row>=arr.length) {
return console.log(arr);
}
for (let col=0; col<arr.length; col++) {
if (!isConflict(arr, row, col)) {
arr[row] = col;
queen(arr, row+1);
}
}
}

function isConflict(arr, row, col) {
for (let i=row-1; i>=0; i--) {
if (col == arr[i] || (row-i) == Math.abs(col-arr[i]))
return true;
}
return false;
}

queen([0,0,0,0,0,0,0,0], 0);

0 comments on commit f240b8b

Please sign in to comment.