Last active
August 10, 2022 12:49
-
-
Save jthomas/2b33224927c70b4aa612 to your computer and use it in GitHub Desktop.
Solving "N Queens Problem" with JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var iterations = 0 | |
var print_board = function (columns) { | |
var n = columns.length, row = 0, col = 0 | |
while (row < n) { | |
while (col < n) { | |
process.stdout.write(columns[row] === col ? 'Q ' : '# ') | |
col++ | |
} | |
process.stdout.write('\n') | |
col = 0 | |
row++ | |
} | |
} | |
var has_conflict = function (columns) { | |
var len = columns.length, last = columns[len - 1], previous = len - 2 | |
while (previous >= 0) { | |
if (columns[previous] === last) return true | |
if (last - (len - 1) === columns[previous] - previous) return true | |
if (last + (len - 1) === columns[previous] + previous) return true | |
previous-- | |
} | |
return false | |
} | |
var place_next_queen = function (total, queens, columns) { | |
if (queens === 0) return columns | |
columns = columns || [] | |
for (var column = 0; column < total; column++) { | |
columns.push(column) | |
iterations++ | |
if (!has_conflict(columns) && | |
place_next_queen(total, queens - 1, columns)) { | |
return columns | |
} | |
columns.pop(column) | |
} | |
return null | |
} | |
print_board(place_next_queen(28, 28)) | |
console.log('\niterations: ', iterations) |
The printBoard function can be written more simple:
var print_board = function (columns) {
for (let i = 0; i < columns.length; i++) {
for (let j = 0; j < columns.length; j++) {
process.stdout.write(columns[i] === j ? "Q " : "# ");
}
process.stdout.write("\n");
}
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please explain your algorithm?