不管怎么去解决迷宫问题,回溯法都是一个经典解法。
这里加了一个简单的图形界面。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Maze extends JApplet {
/**
*
*/
private static final long serialVersionUID = 1L;
private Cell[][] board = new Cell[8][8];
private JButton jbtFindPath = new JButton("Find Path");
private JButton jbtClearPath = new JButton("Clear Path");
private JPanel jpBoard, jpButton;
private JLabel jlblStatus = new JLabel();
public void init() {
jpBoard = new JPanel();
jpBoard.setLayout(new GridLayout(8, 8, 2, 2));
jpButton = new JPanel();
jpButton.setLayout(new FlowLayout());
jpButton.add(jbtFindPath);
jpButton.add(jbtClearPath);
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
board[row][col] = new Cell();
jpBoard.add(board[row][col]);
}
}
setLayout(new BorderLayout());
add(jlblStatus, BorderLayout.NORTH);
add(jpBoard, BorderLayout.CENTER);
add(jpButton, BorderLayout.SOUTH);
jbtFindPath.addActionListener(new Listener());
jbtClearPath.addActionListener(new