0% found this document useful (0 votes)
5 views12 pages

Maze Solving N Queens

The N Queens problem involves placing N chess queens on an N×N chessboard so that no two queens attack each other, and it can be solved using backtracking. The algorithm involves placing queens column by column, checking for clashes, and backtracking if necessary. Optimizations can be made by utilizing properties of diagonals to reduce the number of checks needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Maze Solving N Queens

The N Queens problem involves placing N chess queens on an N×N chessboard so that no two queens attack each other, and it can be solved using backtracking. The algorithm involves placing queens column by column, checking for clashes, and backtracking if necessary. Optimizations can be made by utilizing properties of diagonals to reduce the number of checks needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

N QUEENS

N-QUEENS PROBLEM

Introduction

N Queens is another example problem that can be solved using Backtracking

The N Queens is the problem of placing N chess queens on an N×N chessboard so


that no two queens attack each other

The solution can be a matrix indicating the position of the queens or the index
numbers of the cells in whicb the queens have been placed
N-QUEENS PROBLEM

Backtracking Method of solving


The idea is to place queens one by one in different columns, starting from the leftmost
column.

When we place a queen in a column, we check for clashes with already placed
queens.

In the current column, if we find a row for which there is no clash, we mark this row
and column as part of the solution.

If we do not find such a row due to clashes then we backtrack and return false.
N-QUEENS PROBLEM

Backtracking Algorithm
1) Start in the leftmost column

2) If all queens are placed return true

3) Try all rows in the current column.


Do following for every tried row.
a) If the queen can be placed safely in this row then mark this [row, column] as part
of the solution and recursively check if placing queen here leads to a solution.
b) If placing the queen in [row, column] leads to a solution then return true.
c) If placing queen doesn't lead to a solution then unmark this [row, column]
(Backtrack) and go to step (a) to try other rows.

3) If all rows have been tried and nothing worked, return false to trigger backtracking.
N-QUEENS PROBLEM

Program

nqueens1.java

Sample IO
Input
8

Output
N-QUEENS-01

public static boolean isSafe(char board[][],


import java.util.Scanner; int row, int column, int n)
public class NQueens {
{ int i,j;
public static void main(String[] args) for(i=0;i<column;i++)
{ {
Scanner sc=new Scanner(System.in); if(board[row][i]=='Q')
int n=sc.nextInt(); return false;
char board[][]=new char[n][n]; }
for(int i=0;i<n;i++) for(i=row,j=column; i>=0 && j>=0;i--,j--)
for(int j=0;j<n;j++) {
board[i][j]='-'; if(board[i][j]=='Q')
if(solveNQueens(board,0,n)) return false;
solution(board,n);
else for(i=row,j=column; i<n && j>=0;i++,j--)
System.out.println("No solution exists"); {
} if(board[i][j]=='Q')
public static void solution(char board[][], int return false;
n) }
{ return true;
for(int i=0;i<n;i++) }
{
for(int j=0;j<n;j++)
System.out.print(" "+board[i][j]+" ");
System.out.println();
N-QUEENS-01

public static boolean solveNQueens(char


board[][], int column, int n)
{
if(column>=n)
return true;
for(int i=0;i<n;i++)
{
if(isSafe(board,i,column,n))
{
board[i][column]='Q';
if(solveNQueens(board, column+1,n))
return true;
board[i][column]='-';
}
}
return false;
}
}
N-QUEENS PROBLEM

Optimization

The idea is not to check every element in right and left diagonal instead use
property of diagonals:

1. The sum of i and j is constant and unique for each right diagonal where i is
the row of element and j is the column of element.

2. The difference of i and j is constant and unique for each left diagonal
where i and j are row and column of element respectively.
N-QUEENS PROBLEM

Program

nqueens2.java

The board size is fixed to 4 here.


N-QUEENS-02

import java.util.*; if (solveNQUtil(board, col + 1))


class EthCode { return true;
static int N = 4; board[i][col] = 0; // BACKTRACK
static int[] ld = new int[30];
ld[i - col + N - 1] =
static int[] rd = new int[30];
static int[] cl = new int[30]; rd[i + col] = cl[i] = 0;
static void printSolution(int board[][]) { }
for (int i = 0; i < N; i++) { }
for (int j = 0; j < N; j++) return false;
System.out.printf(" %d ", board[i][j]); }
System.out.printf("\n");
static boolean solveNQ() {
}
} int board[][] ={{ 0, 0, 0, 0 },
static boolean solveNQUtil(int board[][], int col) { { 0, 0, 0, 0 },
if (col >= N) { 0, 0, 0, 0 },
return true; { 0, 0, 0, 0 }};
for (int i = 0; i < N; i++) { if (solveNQUtil(board, 0) == false) {
if ((ld[i - col + N - 1] != 1 && System.out.printf("Solution does not
rd[i + col] != 1) && cl[i] != 1) { exist");
board[i][col] = 1; return false;
ld[i - col + N - 1] = }
rd[i + col] = cl[i] = 1; printSolution(board);
return true;
}
public static void main(String[] args) {
solveNQ();
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

[email protected] +91 7815 095 +91 9019 921 340


095

You might also like