微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

广度优先搜索的时间过长,无法解决迷宫问题

如何解决广度优先搜索的时间过长,无法解决迷宫问题

我有一个大而开放的迷宫,像这样:

############################################################
#.....................#...#................#.........#.....#
#..##.......x.........#.#.#...#.........x..#......#..#.....#
#...#.......#....#....#.#.#...#...####.....####...#..#####.#
#.....###...#....#....###.#...#.........#.........#......#.#
####.#..#...#....#........#...#####..#..#...#######..##..#.#
#....##.....#....#................#..####......#...........#
#......##...######........x....................#......######
#....##........................................#...........#
#.........##########...############........#...#...####....#
#.....#...#..#..#..#......#.......#........#...#....###....#
#######...#..#..#..#......#.......#.....####...............#
#.....#...#..#..#..#......#.......#........................#
#.....#####..#..#..#......#.......######............x......#
#.........................#................................#
#..................x......#..##..........#####.............#
#.........................####.............................#
#..........................................................#
#....##.....#....#................#..####......#...........#
#.....s##...######.............................#......######
#....##........................................#...........#
#.........##########...############........#...#...####....#
#.....#...#..#..#..#......#.......#........#...#....###....#
#######...#..#..#..#......#.......#...x.####...............#
#.....#...#..#...x.#......#.......#.................#......#
#.....#####..#..#..#...#..###....#######............######.#
#...............#..x...#..#.....##...........####...#......#
#...............#......#.........#.........##...#..........#
#...............#......#.........#..........#............#.#
############################################################

“ s”是起点。并且有多个目的地点“ x”。我只需要找到一个目的地。如果目的地接近起点,则BFS算法可以很快找到解决方案。如果它们像上面的示例中那样离得很远,则将花费无尽的时间。 所以我的问题是: a)该算法对这种特定类型的迷宫不利吗?我应该使用A *还是类似的东西? b)我的实现不好吗?

实施:

public class BFS {

    public static String getPath(String[][] map) {
        String[] ways = { "L","R","U","D" }; // directions to go
        Queue<String> q = new LinkedList<>();
        q.offer("");
        String path = "";

        while (!foundBait(map,path)) {
            path = q.poll();

            for (String s : ways) {
                String newPath = path + s;
                if (valid(map,newPath))
                    q.offer(newPath);
            }
        }
        return path;
    }

    private static boolean foundBait(String[][] map,String moves) {
        int xStart = 0;
        int yStart = 0;

        for (int y = 0; y < map.length; y++)
            for (int x = 0; x < map[0].length; x++)
                if (map[y][x].equals("s")) {
                    xStart = x;
                    yStart = y;
                }

        int i = xStart;
        int j = yStart;

        for (int s = 0; s < moves.length(); s++) {

            if (moves.charat(s) == "L".charat(0))
                i--;
            else if (moves.charat(s) == "R".charat(0))
                i++;
            else if (moves.charat(s) == "U".charat(0))
                j--;
            else if (moves.charat(s) == "D".charat(0))
                j++;

        }

        if (map[j][i].equals("x"))
            return true;

        return false;
    }

    private static boolean valid(String[][] map,String moves) {
        int xStart = 0;
        int yStart = 0;

        for (int y = 0; y < map.length; y++)
            for (int x = 0; x < map[0].length; x++)
                if (map[y][x].equals("s")) {
                    xStart = x;
                    yStart = y;
                }

        int i = xStart;
        int j = yStart;

        for (int s = 0; s < moves.length(); s++) {

            if (moves.charat(s) == "L".charat(0))
                i--;
            else if (moves.charat(s) == "R".charat(0))
                i++;
            else if (moves.charat(s) == "U".charat(0))
                j--;
            else if (moves.charat(s) == "D".charat(0))
                j++;

            if (!(0 <= i && i < map[0].length && 0 <= j && j < map.length))
                return false;
            else if (map[j][i].equals("#") || map[j][i].equals("-"))
                return false;

        }
        return true;
    }

}

解决方法

如评论中所述,问题不是标记添加到路径的节点,解决方案是使用第二个矩阵进行标记。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。