Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions N-Queen(FSS)108.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// Way to solve the N-Queen problem

import java.util.Arrays;

class Main
{
// Function to check if two queens threaten each other or not
private static boolean isSafe(char[][] mat, int r, int c)
{
// return false if two queens share the same column
for (int i = 0; i < r; i++)
{
if (mat[i][c] == 'Q') {
return false;
}
}

// return false if two queens share the same `` diagonal
for (int i = r, j = c; i >= 0 && j >= 0; i--, j--)
{
if (mat[i][j] == 'Q') {
return false;
}
}

// return false if two queens share the same `/` diagonal
for (int i = r, j = c; i >= 0 && j < mat.length; i--, j++)
{
if (mat[i][j] == 'Q') {
return false;
}
}

return true;
}

private static void printSolution(char[][] mat)
{
for (char[] chars: mat) {
System.out.println(Arrays.toString(chars).replaceAll(",", ""));
}
System.out.println();
}

private static void nQueen(char[][] mat, int r)
{
// if `N` queens are placed successfully, print the solution
if (r == mat.length)
{
printSolution(mat);
return;
}

// place queen at every square in the current row `r`
// and recur for each valid movement
for (int i = 0; i < mat.length; i++)
{
// if no two queens threaten each other
if (isSafe(mat, r, i))
{
// place queen on the current square
mat[r][i] = 'Q';

// recur for the next row
nQueen(mat, r + 1);

// backtrack and remove the queen from the current square
mat[r][i] = '–';
}
}
}

public static void main(String[] args)
{
// `N × N` chessboard
int N = 8;

// `mat[][]` keeps track of the position of queens in
// the current configuration
char[][] mat = new char[N][N];

// initialize `mat[][]` by `-`
for (int i = 0; i < N; i++) {
Arrays.fill(mat[i], '–');
}

nQueen(mat, 0);
}
}