Skip to content

Instantly share code, notes, and snippets.

@iJustErikk
Created December 11, 2020 00:47
Show Gist options
  • Save iJustErikk/9d904ef65222489bfeaa6c87988f1b60 to your computer and use it in GitHub Desktop.
Save iJustErikk/9d904ef65222489bfeaa6c87988f1b60 to your computer and use it in GitHub Desktop.
SOO HAPPY I DID THIS ONE
const printBoard = (board) => {
const length = board.length;
for (let row = 0; row < length; row++) {
let str = "";
for (let col = 0; col < length; col++) {
const current = board[row][col];
current ? str += "Q" : str += "X";
}
console.log(str);
}
console.log("");
}
const nQueens = (n) => {
const board = new Array(n).fill().map(() => new Array(n).fill(false));
nQueensSolver(board);
printBoard(board);
};
// place in currentRow
// iterate through row and find a spot to place
const nQueensSolver = (board, currentRow = 0) => {
if (currentRow === board.length) return true;
for (let currentCol = 0; currentCol < board.length; currentCol++) {
board[currentRow][currentCol] = true;
if (isValidBoard(board)) {
if(nQueensSolver(board, currentRow + 1)) {
return true;
}
board[currentRow][currentCol] = false;
} else {
board[currentRow][currentCol] = false;
}
}
return false;
};
const isValidBoard = (boardToValidate) => {
const length = boardToValidate.length;
const checkCol = (col) => {
let numQueens = 0;
for (let row = 0; row < length; row++) {
if (boardToValidate[row][col]) numQueens++;
}
return numQueens < 2;
};
const upperLeft = (row, col) => {
// move up and left until we hit wall
// returns num of queens
let currentRow = row;
let currentCol = col;
let numQueens = 0;
while (currentRow > -1 && currentCol > -1) {
if (boardToValidate[currentRow][currentCol]) numQueens++;
currentCol--;
currentRow--;
}
return numQueens;
};
const upperRight = (row, col) => {
// move up and right until we hit wall
let currentRow = row;
let currentCol = col;
let numQueens = 0;
while (currentRow > -1 && currentCol < length) {
if (boardToValidate[currentRow][currentCol]) numQueens++;
currentCol++;
currentRow--;
}
return numQueens;
};
const lowerLeft = (row, col) => {
// move down and left until we hit wall
let currentRow = row;
let currentCol = col;
let numQueens = 0;
while (currentRow < length && currentCol > -1) {
if (boardToValidate[currentRow][currentCol]) numQueens++;
currentCol--;
currentRow++;
}
return numQueens;
};
const lowerRight = (row, col) => {
// move down and right until we hit wall
let currentRow = row;
let currentCol = col;
let numQueens = 0;
while (currentRow < length && currentCol < length) {
if (boardToValidate[currentRow][currentCol]) numQueens++;
currentCol++;
currentRow++;
}
return numQueens;
};
// # queen 0 no queen
// # 0 0 0
// 0 0 # 0
// 0 0 0 0
// 0 0 0 0
const checkDiagonal = (row, col) => {
return (
upperLeft(row - 1, col - 1) +
upperRight(row - 1, col + 1) +
lowerLeft(row + 1, col - 1) +
lowerRight(row + 1, col + 1) +
1 <
2
);
};
for (let row = 0; row < length; row++) {
for (let col = 0; col < length; col++) {
const isQueen = boardToValidate[row][col];
if (isQueen) {
if (!checkCol(col)) return false;
if (!checkDiagonal(row, col)) return false;
}
}
}
return true;
};
// XQXX
// XXXQ
// QXXX
// XXQX
nQueens(4);
// QXXXXXXX
// XXXXQXXX
// XXXXXXXQ
// XXXXXQXX
// XXQXXXXX
// XXXXXXQX
// XQXXXXXX
// XXXQXXXX
nQueens(8);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment