广度搜索使用队列的第一个迷宫求解器

时间:2022-06-03 18:27:15

I have to write a program that follows a breadth first search using queues to solve a maze. I think I almost have it done but when I run it, it says it does not contain a main type. Also can some one explain to me what is wrong with the code on line 47 and 48 (i = current.i and j=current.j) it should be fine but i get an error message that says it "cannot be resolved or is not in field, i though declaring above the while loop would be fine. It is also not critical but me teacher asked for us to use a repaint() method so we can see the maze solve itself. I'm not really sure how to use thus method but I have included that code as well. Any help to get this program running would be greatly appreciated.

我必须编写一个程序,该程序遵循广泛的第一次搜索,使用队列来解决迷宫问题。我想我几乎完成了它,但是当我运行它时,它说它不包含主类型。也可以有人向我解释第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();
}

and here is the method we are supposed to use for repaint(), this part isn't crucial but some help or an explanation of how it works would be great.

这是我们应该用于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);
    }
}

1 个解决方案

#1


0  

I think the reason your program cannot be run because you've declared your main function as this

我认为你的程序无法运行的原因是因为你已经宣布你的主要功能

public static void main(String args) 

but it actually should have an array of Strings as the argument. ie. this

但它实际上应该有一个字符串数组作为参数。即。这个

public static void main(String[] args)

#1


0  

I think the reason your program cannot be run because you've declared your main function as this

我认为你的程序无法运行的原因是因为你已经宣布你的主要功能

public static void main(String args) 

but it actually should have an array of Strings as the argument. ie. this

但它实际上应该有一个字符串数组作为参数。即。这个

public static void main(String[] args)