[英]breadth search first maze solver using queues
我必须编写一个程序,该程序使用队列来解决迷宫,然后进行广度优先搜索。 我想我几乎完成了,但是运行时它说它不包含主类型。 也可以有人向我解释第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);
}
}
我认为您的程序无法运行的原因是您已经声明了main函数为this
public static void main(String args)
但实际上应该有一个字符串数组作为参数。 即。 这个
public static void main(String[] args)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.