Can someone explain what is going on in this program line by
Can someone explain what is going on in this program line by line to get a better understanding of the program. The program is a eight queen problem so i am trying to understand what the program is doing line at a time could you please add comments so it is easier to understand. Java netbeans program Thank you
public class Board {
public static void main(String args[]) {
int N = 8;
int[][] board = new int[N][N];
solve(0, board, N);
for(int i = 0; i < N; i++) {
for(int j = 0; j < N; j++) {
if(board[i][j]==1) System.out.print(\"Q \");
else System.out.print(\" _\");
}
System.out.println();
}
}
static boolean solve(int row, int[][] board, int N) {
if(row>=N) return true;
for(int position = 0; position < N; position++) {
if(isValid(board, row, position, N)) {
board[row][position] = 1;
if(!solve(row+1, board, N)) {
board[row][position] = 0;
} else
return true;
}
}
return false;
}
static boolean isValid(int[][] board, int x, int y, int N) {
int i, j;
for(i = 0; i < x; i++)
if(board[i][y]==1)
return false;
i = x - 1;
j = y - 1;
while((i>=0)&&(j>=0))
if(board[i--][j--]==1) return false;
i = x - 1;
j = y + 1;
while((i>=0)&&(j<N))
if(board[i--][j++]==1) return false;
return true;
}
}
Solution
public class Board // class name
{
public static void main(String args[])
{
int N = 8;
int[][] board = new int[N][N]; // 8 * 8 matrix
solve(0, board, N); //slove function declaration
for(int i = 0; i < N; i++)
{
for(int j = 0; j < N; j++)
{
if(board[i][j]==1) System.out.print(\"Q \"); // if condition is true Q will be printed otherwise it will print _
else System.out.print(\" _\");
}
System.out.println(); // this is used for next line.
}
}
static boolean solve(int row, int[][] board, int N) // solve function definition & here boolean means it is true it is execute body of the function
// otherwise it returns false.
{
if(row>=N) return true; // if condition is true it returns true otherwise it goes to the for loop
for(int position = 0; position < N; position++) // if condition is true it goes to the body of the loop , otherwise it exit from the loop
{
if(isValid(board, row, position, N)) // isValid is true it goes to the if part.
{
board[row][position] = 1;
if(!solve(row+1, board, N)) // here solve function is true , here it returns 1 and here we use ! not symbol it retuns zero. i.e it goes to else part.
// here solve function is false , here it returns 0 and here we use ! not symbol it retuns one. i.e it goes to if part.
{
board[row][position] = 0;
}
else
return true;
}
}
return false;
}
static boolean isValid(int[][] board, int x, int y, int N) // isValid function definition
{
int i, j;
for(i = 0; i < x; i++) // if condtion is true for loop is executed.
if(board[i][y]==1) //if this condition is true it returns false. otherwise it goes to the next statement.
return false;
i = x - 1;
j = y - 1;
while((i>=0)&&(j>=0)) // here while is true it goes to the next statement.
if(board[i--][j--]==1) //if this condition is true it returns false. otherwise it goes to the next statement.
return false;
i = x - 1;
j = y + 1;
while((i>=0)&&(j<N)) // here while is true it goes to the next statement
if(board[i--][j++]==1) //if this condition is true it returns false. otherwise it goes to the next statement.
return false;
return true;
}
}

