Consider the problem of trying to place 8 queens on a chess board such that no q
ID: 3676557 • Letter: C
Question
Consider the problem of trying to place 8 queens on a chess board such that no queen can attack another queen
» Suppose we have a Board claSs WI Suppose we have a Board class with the following methods: Method/Constructor Description public Board (int size) construct empty board public boolean isSafe (int row, int column) true if queen can be public void place (int row, int column) public void remove (int row, int column)r public String toString () safely placed here place queen here remove queen from here text display of board Write a method solveQueens that accepts a Board as a parameter and tries to place 8 queens on it safely. » Write a method solveQueens that acce Your method should stop exploring if it finds a solution.Explanation / Answer
public static void solveQueens(Board board)
{
if (solveQueens(board, 1))
{
System.out.println("solution as follows:");
System.out.println(board);
}
else
{
System.out.println("HUff no solution found.");
}
}
public static boolean solveQueens(Board board, int col)
{
if (col > board.size())
{
return true;
}
else
{
for (int row = 1; row <= board.size(); row++)
{
if (board.isSafe(row, col))
{
board.place(row, col);
if (explore(board, col + 1))
{
return true;
}
b.remove(row, col);
}
}
return false;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.