N-Queen : Check if possible to place in NxN

ZeeshanAli-0704 - Aug 21 '22 - - Dev Community

const N = 4;

function printSolution(board) {
  console.log(board);
}

function isSafe(board, row, col) {
  for (let i = 0; i < col; i++) {
    if (board[row][i] == 1) return false;
  }

  for (i = row, j = col; i >= 0 && j >= 0; i--, j--)
    if (board[i][j]) return false;

  for (i = row, j = col; j >= 0 && i < N; i++, j--)
    if (board[i][j]) return false;

  return true;
}

function solveNQUtil(board, col) {
  if (col >= N) return true;

  for (let i = 0; i < N; i++) {
    if (isSafe(board, i, col) == true) {
      board[i][col] = 1;
      if (solveNQUtil(board, col + 1) == true) return true;
      board[i][col] = 0;
    }
  }

  return false;
}
function solveNQ() {
  let board = [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
  ];

  if (solveNQUtil(board, 0) == false) {
    console.log("Solution does not exist");
    return false;
  }

  printSolution(board);
  return true;
}


console.log(solveNQ());




Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player