Question
JAVA - 8ht Queens - only 1 and only symetrical solution necessrary + given parti of code. Please finish code to be as image and
JAVA - 8ht Queens - only 1 and only symetrical solution necessrary + given parti of code. Please finish code to be as image and explain code as max possible.
int chessBoard[][] = {{ 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 }, { 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 }}; --understandable
public class Main {
static void printMatrix(int chessBoard[][]) ----understandable
{ for (int outer = 0; outer
for (int inner = 0; inner
System.out.print(" " + chessBoard[outer][inner]+ " ");
System.out.println();
}//end inner loop
}//end outer loop
}//end method
static boolean checkStep(int chessBoard[][], int rows, int cols) { --confused why there are passed rows and cols, maybe it is easer to re-edit code?
int l, m; for (l = 0; l
if (chessBoard[rows][l] == 1)
return false;
} //end for
for (l = rows, j = cols;l >= 0 && m >= 0; l--, m--){
if (chessBoard[l][m] == 1)
return false;
}//end for
for (l = rows, m = cols; m >= 0 && l
if (chessBoard[l][m] == 1)
return false;
}//end for
return true;
}//end method
public static boolean solve8Queen(int chessBoard[][], int cols) { --please explain full method step by step
if (col >= 8){
return true;
}
for (int l = 0; l
if (checkStep(chessBoard, l, cols)) {
chessBoard[l][cols] = 1;
if (solve8Queen(chessBoard, cols + 1)){
return true;
}
chessBoard[i][cols] = 0;
}
}
return false;
}
public static void main(String args[]){
int chessBoard[][] = { { 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 }, { 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 }};
//call the solution
if (!solve8Queen(chessBoard, 0)) { -- how it could be easier to call out method?
System.out.print("Solution not found");
return;
}
printMatrix(chessBoard); }//end main method }//end class
so yes, if there is easier version to re-redit code - please, up to you. Only 1 possible solution necessary not for N queens.
p.s. code from chegg
The only symmetrical solution to the eight queens puzzle (up to rotation and reflection)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started