From 3980f8703c6b06efb07aab56a9c9ed2aefc27878 Mon Sep 17 00:00:00 2001 From: Mayank Date: Thu, 30 Oct 2025 22:03:42 +0530 Subject: [PATCH] Added N-Queens Problem (Backtracking) in JavaScript with clear comments --- Algorithms/N-QueensProblem.js | 57 +++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 Algorithms/N-QueensProblem.js diff --git a/Algorithms/N-QueensProblem.js b/Algorithms/N-QueensProblem.js new file mode 100644 index 0000000..0654648 --- /dev/null +++ b/Algorithms/N-QueensProblem.js @@ -0,0 +1,57 @@ +// N-Queens problem using backtracking +function solveNQueens(n) { + const board = Array.from({ length: n }, () => Array(n).fill(".")); // Create n×n board + const result = []; + + // Helper function to check if placing a queen is safe + function isSafe(row, col) { + // Check the same column above + for (let i = 0; i < row; i++) { + if (board[i][col] === "Q") return false; + } + + // Check upper left diagonal + for (let i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) { + if (board[i][j] === "Q") return false; + } + + // Check upper right diagonal + for (let i = row - 1, j = col + 1; i >= 0 && j < n; i--, j++) { + if (board[i][j] === "Q") return false; + } + + return true; // Safe position + } + + // Backtracking function + function solve(row) { + // Base case: all queens are placed + if (row === n) { + // Push a copy of current board to result + result.push(board.map(r => r.join(""))); + return; + } + + // Try placing queen in each column of current row + for (let col = 0; col < n; col++) { + if (isSafe(row, col)) { + board[row][col] = "Q"; // Place queen + solve(row + 1); // Move to next row + board[row][col] = "."; // Backtrack (remove queen) + } + } + } + + solve(0); // Start from row 0 + return result; +} + +// Example usage +const n = 4; +const solutions = solveNQueens(n); + +console.log(`Total solutions for ${n}-Queens: ${solutions.length}`); +solutions.forEach((sol, i) => { + console.log(`\nSolution ${i + 1}:`); + sol.forEach(row => console.log(row)); +});