Skip to content

Commit

Permalink
add source code
Browse files Browse the repository at this point in the history
  • Loading branch information
deathnopool authored Oct 26, 2019
1 parent 7bf4190 commit 299f20d
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions 8Queens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
let solutionCount = 0;
function countPrint() {
return ++solutionCount;
}

function buildArrs() {
let arrs = [];
for (let i=8; i>0; i--) {
let arr = [];
for (let j=8; j>0; j--) {
arr.push(0);
}
arrs.push(arr);
}
return arrs;
}

function printArrs(arrs) {
console.log("------------------------------ count=", countPrint());
for (let i=0; i<arrs.length; i++) {
let arr = arrs[i];
let row = arr.map(item => {
return !!item ? ' ● ' : ' ○ ';
}).join("");
console.log(row);
}
}

function isConflict(arrs, rowIndex, colIndex) {
const target = arrs[rowIndex][colIndex];
for (let i=rowIndex-1; i>=0; i--) {
let row = arrs[i];
let j = row.indexOf(1);
if (j==colIndex || (rowIndex-i == Math.abs(colIndex-j)))
return true;
}
return false;
}

function clearDirtyRow(row) {
row.fill(0);
}

function findQueens(arrs, rowIndex) {
if (rowIndex>=8) {
printArrs(arrs);
return;
}
for (let i=0; i<arrs[rowIndex].length; i++) {
if (!isConflict(arrs, rowIndex, i)) {
clearDirtyRow(arrs[rowIndex]);
arrs[rowIndex][i] = 1;
findQueens(arrs, rowIndex+1);
}
}
}

function start() {
let plate = buildArrs();
solutionCount = 0;
findQueens(plate, 0);
}

0 comments on commit 299f20d

Please sign in to comment.