Skip to content

Commit

Permalink
Merge pull request akshitagit#44 from Riyagoel2/master
Browse files Browse the repository at this point in the history
Added Solution to NQueenProblem using Backtracking
  • Loading branch information
akshitagupta15june authored Sep 25, 2020
2 parents 7b6691f + eaca86d commit 55d03a6
Showing 1 changed file with 102 additions and 0 deletions.
102 changes: 102 additions & 0 deletions Recursion&Backtracking/NQueenProblem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// N-Queen Problem using Backtracking
/*
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other.
The solution uses Backtracking to solve the problem.
The expected output will contain all possible positions to place the queens.
*/

import java.util.*;

public class Solution
{

public static void main(String [] args){

Scanner s=new Scanner(System.in);
int N=s.nextInt();
boolean[][] board=new boolean[N][N];
NQueen(board, 0, 0,0, N ,"");

}

public static void NQueen(boolean[][] board, int row, int col, int queensPlaced, int totalQueens, String ans) {

if (queensPlaced == totalQueens) {
System.out.println(ans);
return;
}

if (col == board[0].length) {
col = 0;
row++;
}

if (row == board.length) {
return;
}

if (isItSafeToPlaceQueen(board, row, col)) {
board[row][col] = true;
NQueen(board, row, col + 1, queensPlaced + 1, totalQueens, ans + "[" + row + "-" + col + "]");
board[row][col] = false;
}
NQueen(board, row, col + 1, queensPlaced, totalQueens, ans);

}

private boolean isItSafeToPlaceQueen(boolean[][] board, int row, int col) {


int r = row - 1;
int c = col;
while (r >= 0) {
if (board[r][c]) {
return false;
}
r--;
}

r = row;
c = col - 1;
while (c >= 0) {
if (board[r][c]) {
return false;
}
c--;
}

r = row - 1;
c = col - 1;
while (r >= 0 && c >= 0) {
if (board[r][c]) {
return false;
}
r--;
c--;
}

r = row - 1;
c = col + 1;
while (r >= 0 && c < board[0].length) {
if (board[r][c]) {
return false;
}
r--;
c++;
;
}
return true;
}

}


/* Input :
4
Output :
[0-1][1-3][2-0][3-2]
[0-2][1-0][2-3][3-1]
*/

0 comments on commit 55d03a6

Please sign in to comment.