我正在编写一个程序来递归地解决八皇后难题,但是我很难弄清楚我应该如何让我的程序“回溯”来解决问题。我的 Solve() 方法和我的 isOpen() 方法可能都是错误的,但我无法弄清楚我做错了什么。我在下面有我的代码(和驱动程序)。对不起,它太长了,但如果我能得到一些提示,我会很感激。
注意* 图片图标只是 Freddie Mercury 的图片
import java.io.*;
import java.util.Formatter;
import javax.swing.*;
import java.awt.*;
public class EightQueens extends JFrame {
// declare class constants as needed
final int BOARD_DIMENSION = 8;
final boolean[][] BOARD = new boolean[8][8];
// declare instance variables
PrintWriter output;
int backtrack;
int comparison;
File file;
// gui components
JPanel chessPanel;
JPanel square[];
Icon queenIcon;
JLabel imageLabel;
// constructor method
public EightQueens() throws FileNotFoundException {
// gui stuff
setTitle("8 Queens: Freddie Style");
setPreferredSize(new Dimension(800, 800));
chessPanel = new JPanel(new GridLayout(8, 8));
chessPanel.setPreferredSize(new Dimension(100, 100));
chessPanel.setBackground(Color.BLACK);
square = new JPanel[64];
queenIcon = new ImageIcon(getClass().getResource(
"/resource/mercury.jpg"));
JLabel imageLabel = new JLabel("", queenIcon, JLabel.CENTER);
// CREATE AND INITIALIZE THE OUTPUT FILE
file = new File("TraceQueens.txt");
output = new PrintWriter(file);
// CREATE AND INITIALIZE BOARD
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
BOARD[i][j] = false;
// add the squares to the chess panel
for (int i = 0; i < square.length; i++)// adds chess squares to the
// board
{
square[i] = new JPanel(new BorderLayout());
square[i].setBackground(Color.BLACK);
chessPanel.add(square[i]);
}
// this will make the squares the correct colors
for (int i = 0; i < BOARD_DIMENSION; i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = BOARD_DIMENSION + 1; i < (2 * BOARD_DIMENSION)
&& i > BOARD_DIMENSION; i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (2 * BOARD_DIMENSION); i < (3 * BOARD_DIMENSION)
&& i >= (2 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (3 * BOARD_DIMENSION) + 1; i < (4 * BOARD_DIMENSION)
&& i > (3 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (4 * BOARD_DIMENSION); i < (5 * BOARD_DIMENSION)
&& i >= (4 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (5 * BOARD_DIMENSION) + 1; i < (6 * BOARD_DIMENSION)
&& i > (5 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (6 * BOARD_DIMENSION); i < (7 * BOARD_DIMENSION)
&& i >= (6 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
for (int i = (7 * BOARD_DIMENSION) + 1; i < (8 * BOARD_DIMENSION)
&& i > (7 * BOARD_DIMENSION); i = i + 2)
square[i].setBackground(Color.WHITE);
// puts the chess panel on the EightQueens frame
add(chessPanel);
} // end constructor
// user interface method used to wrap the parameter representing the first
// column
public boolean solve() {
return solve(0);
}
// recursive method solve
public boolean solve(int column) {
// The Queen has been placed on the board in column
int row = 0;
BOARD[row][column] = true;
output.println("The queen has been placed at [0][" + column + "]");
// if column is beyond the last column, then the problem is solved
// base case
for(int r = 0; r < BOARD_DIMENSION; r++);
if (column == BOARD_DIMENSION) {
output.println("The problem has been solved");
output.println("The program backtracked " + backtrack
+ " times and made " + comparison + " comparisons");
// CLOSE FILE
output.close();
// RETURN TRUE
return true;
} else // attempt a solution from column
{
output.println("Attempting solution from column " + column);
for (int i = 0; i < BOARD_DIMENSION; i++) {
if (isOpen(i, column)) {
BOARD[i][column] = true;
comparison++;
// solve the "rest of the board"
if (solve(column + 1)) {
output.println("The queen has beel placed at [" + i
+ "] + [" + column + "]");
square[((i * 2) + column)].add(imageLabel,
BorderLayout.CENTER);// this converts the 2-d
// array location into a
// single value to
// make it compatible with the gui representation of the
// board
return true;
}
else {
BOARD[i][column] = false;
output.println("the queen has been removed from ["+i+"]["+column+"]");
}
} else
output.println("The queen cannot be placed at [" + i
+ "][" + column + "]");
}
backtrack++;
//output.close(); //This is for testing only
return false;
// return false to Backtrack to previous column since no squares in
// current row will solve problem
}
}
// Method to verify that the square is open.
public boolean isOpen(int row, int column) {
{
for (int i = 0; i < BOARD_DIMENSION; i++)// tests for the same row
{
if (BOARD[row][i] == true)
return false;
}
for (int i = 0; i < BOARD_DIMENSION; i++)// test for the same column
{
if (BOARD[i][column] == true)
return false;
}
for (int i = 0; i < BOARD_DIMENSION; i++)
// test for diagonal down to the left
while (row - 1 >= 0 && column - 1 >= 0) {
if (BOARD[row - 1][column - 1] == true)
return false;
}
for (int i = 0; i < BOARD_DIMENSION; i++)
// test for diagonal up to the left
while (row + 1 < BOARD_DIMENSION && column - 1 >= 0)
if (BOARD[row + 1][column - 1] == true)
return false;
}
return true;
}
// output the board
public void printBoard()// this is probably wrong, but
// I want to find my mistakes in
// the rest of the code before fixing it
{
for (int i = 0; i < BOARD_DIMENSION; i++)
for (int j = 0; j < BOARD_DIMENSION; j++) {
if (BOARD[i][j] == false) {
System.out.print("*");
if (i == BOARD_DIMENSION - 1)
System.out.print("\n");
}
else
System.out.print("Q");
}
}
} // end class EightQueens
司机:
import javax.swing.*;
import java.io.*;
import java.util.*;
public class EightQueensGame {
public static void main(String args[]) throws FileNotFoundException {
EightQueens eightQueens = new EightQueens();
eightQueens.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
eightQueens.pack();
eightQueens.setVisible(true);//just for testing
if (eightQueens.solve())
{
eightQueens.setVisible(true);
eightQueens.printBoard();
}
else
System.out.println("Sorry, there is no solution");
}
}