using an 8 8 int array to represent a chess board write a r
using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eight queens are placed on a chess board where no queens can attack one another. Netbeans source code, Please explain the code to get a better understanding.
Solution
/* This is a Java program to solve N Queen Problem using
Backtracking and recursive function */
public class NQueens
{
final int N = 8;
/* This is basically a utility function to print solution */
void toprint(int board[][])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
System.out.print(\" \" + board[i][j]
+ \" \");
System.out.println();
}
}
/*This is basically a utility function to check if a queen can
be placed on board[rows][cols]. This
function is called when \"col\" queens are already
placeedin columns from 0 to cols -1. As a result we need
to check only left side for attacking queens */
boolean isSafefunction(int board[][], int rows, int cols)
{
int i, j;
/* This is used to check this row on left side */
for (i = 0; i < cols; i++)
if (board[rows][i] == 1)
return false;
/*This is used to check upper diagonal on left side */
for (i=rows, j=cols; i>=0 && j>=0; i--, j--)
if (board[i][j] == 1)
return false;
/* This is used to check lower diagonal on left side */
for (i=rows, j=cols; j>=0 && i<N; i++, j--)
if (board[i][j] == 1)
return false;
return true;
}
/* This is a A recursive function to solve N
Queen problem */
boolean tosolveNQueenUtility(int board[][], int column)
{
/* This is the bbase case: If all queens are placed
then return true */
if (column >= N)
return true;
/*Now we will consider this column and try placing
this queen in all rows one by one */
for (int i = 0; i < N; i++)
{
/* Next we will Check if queen can be placed on
board[i][column] */
if (isSafefunction(board, i, column))
{
/* now we will place this queen in board[i][column] */
board[i][column] = 1;
/*we will now recur to place rest of the queens */
if (tosolveNQueenUtility(board, column + 1) == true)
return true;
/*However now If placing queen in board[i][column]
doesn\'t lead to a solution then we will be
removing queen from board[i][col] */
board[i][column] = 0; // This is called BACKTRACKING
}
}
/*On the other hand, if queen can not be place in any row in
this column , then return false */
return false;
}
/* This function is used to solve the N Queen problem using
Backtracking. Moreover mainly uses tosolveNQueenUtility() to
solve the problem. This functin returns false if queens
cannot be placed, otherwise return true and
prints placement of queens in the form of 1s.
.*/
boolean solveNQueenfunction()
{
int board[8][8] = {{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
if (tosolveNQueenUtility(board, 0) == false)
{
System.out.print(\"Solution does not exist\");
return false;
}
toprint(board);
return true;
}
// This is the driver program which is used to test above function
public static void main(String args[])
{
NQueens Queenhere = new NQueens();
Queenhere.solveNQueenfunction();
}
}
![using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig](/WebImages/1/using-an-8-8-int-array-to-represent-a-chess-board-write-a-r-966967-1761495176-0.webp)
![using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig](/WebImages/1/using-an-8-8-int-array-to-represent-a-chess-board-write-a-r-966967-1761495176-1.webp)
![using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig using an 8 × 8 int[][] array to represent a chess board, write a recursive method which solves the eight queens problem. The goal is to find one state where eig](/WebImages/1/using-an-8-8-int-array-to-represent-a-chess-board-write-a-r-966967-1761495176-2.webp)