我必须编写一个程序,该程序遵循使用队列的广度优先搜索来解决迷宫。我想我差不多完成了,但是当我运行它时,它说它不包含主要类型。也有人可以向我解释第 47 行和第 48 行的代码有什么问题(i = current.i 和 j=current.j)应该没问题,但我收到一条错误消息,说它“无法解决或者是不在现场,我虽然在 while 循环上方声明就可以了。这也不是关键,但我的老师要求我们使用 repaint() 方法,以便我们可以看到迷宫自行解决。我不太确定如何使用这种方法,但我也包含了该代码。任何帮助使该程序运行将不胜感激。
import java.awt.Point;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class MazeSolver {
public static final int WALL = 1, OPEN = 0, GOAL = 2, START = 3;
public static final int ACTIVE = 4, SOLUTION = 5;
private int[][] maze = {
{1, 1, 1, 1, 1, 1, 1, 1},
{1, 3, 0, 0, 0, 0, 0, 1},
{1, 1, 0, 1, 1, 1, 0, 1},
{1, 1, 0, 1, 0, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 0, 1},
{1, 1, 0, 1, 1, 1, 0, 1},
{1, 0, 0, 0, 0, 1, 2, 1},
{1, 1, 1, 1, 1, 1, 1, 1},
};
public static Point startSearch(int[][] grid) {
int x = -1, y = -1;
for (int col = 0; col < grid.length; col++) {
for (int row = 0; row < grid.length; row++) {
if (grid[col][row] == 3) {
x = col;
y = row;
return new Point(x, y);
}
}
}
return null;
}
public static Point[] algorithm(int[][] maze) {
ConcurrentLinkedQueue<Point> path = new ConcurrentLinkedQueue<Point>();
ConcurrentLinkedQueue<Point> predecessor = new ConcurrentLinkedQueue<Point>();
Point start = startSearch(maze);
Point current;
Point north, south, east, west;
int i = 0;
int j = 0;
path.offer(start);
while (!path.isEmpty()) {
current = path.poll();
i = current.i;
j = current.j;
if (i == maze.length - 1 && j == maze.length - 1
&& maze[i][j] == '0') {
Point[] trail = new Point[path.size()];
while (path.isEmpty()) {
for (int k = 0; k < path.size(); k++) {
trail[k] = path.poll();
} return trail;
}
}
east = new Point(i, j + 1);
south = new Point(i + 1, j);
west = new Point(i, j - 1);
north = new Point(i - 1, j);
if (j + 1 >= 0 && j + 1 < maze.length && maze[i][j + 1] == '0'
&& predecessor.contains(east) == false) {
predecessor.offer(east);
path.offer(current);
path.offer(east);
} else if (i + 1 >= 0 && i + 1 < maze.length
&& maze[i + 1][j] == '0'
&& predecessor.contains(south) == false) {
predecessor.offer(south);
path.offer(current);
path.offer(south);
} else if (j - 1 >= 0 && j - 1 < maze.length
&& maze[i][j - 1] == '0'
&& predecessor.contains(west) == false) {
predecessor.offer(west);
path.offer(current);
path.offer(west);
} else if (i - 1 >= 0 && i - 1 < maze.length
&& maze[i - 1][j] == '0'
&& predecessor.contains(north) == false) {
predecessor.offer(north);
path.offer(current);
path.offer(north);
}
}
return null;
}
public int[][] getMaze() {
return maze;
}
public static void main(String args) {
new MazeSolver();
}
这是我们应该用于 repaint() 的方法,这部分并不重要,但一些帮助或解释它是如何工作的会很棒。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JComponent;
public class MazeViewer extends JComponent {
private static final long serialVersionUID = 1L;
private final MazeSolver parent;
public MazeViewer(MazeSolver parent) {
this.parent = parent;
setPreferredSize(new Dimension(500, 500));
}
@Override
public void paintComponent(Graphics graphics) {
int[][] maze = parent.getMaze();
int cellSize = Math.min(getWidth() / maze[0].length, getHeight()
/ maze.length);
graphics.setColor(Color.GRAY);
graphics.fillRect(0, 0, getWidth(), getHeight());
for (int row = 0; row < maze.length; row++)
for (int col = 0; col < maze[0].length; col++)
drawCell(graphics, maze[row][col], row, col, cellSize);
}
private void drawCell(Graphics graphics, int mazeCellValue, int row,
int col, int cellSize) {
switch (mazeCellValue) {
case MazeSolver.WALL:
graphics.setColor(Color.BLACK);
break;
case MazeSolver.OPEN:
graphics.setColor(Color.WHITE);
break;
case MazeSolver.GOAL:
graphics.setColor(Color.RED);
break;
case MazeSolver.START:
graphics.setColor(Color.GREEN);
break;
case MazeSolver.ACTIVE:
graphics.setColor(Color.CYAN);
break;
case MazeSolver.SOLUTION:
graphics.setColor(Color.GREEN);
break;
}
graphics.fillRect(col * cellSize, row * cellSize, cellSize,
cellSize);
graphics.setColor(Color.GRAY); // border
graphics.drawRect(col * cellSize, row * cellSize, cellSize,
cellSize);
}
}