迷宫解决广度优先搜索

时间:2013-05-03 19:45:36

标签: breadth-first-search maze

有人可以解释我怎样才能使用广度优先搜索来解决迷宫问题?我需要使用广度优先搜索来找到迷宫中的最短路径,但我很困惑。

这是我书中的伪代码:

void breadth_first_search(tree T) {
  queue!;
  node u, v;

  initialize(Q);
  v = root of T;
  visit v;
  enqueue(Q, v);

  while (!empty(Q)) {
    dequeue(Q, v);
    for (each child u of v) {
      visit u;
      enqueue(Q, u);
    }
  }
}

因此,如果我有一个存储在2D矩阵中的迷宫,那么“根”(即起点)将会出现在maze[x][y]

2 个答案:

答案 0 :(得分:11)

这是一个完整的BFS迷宫求解器。如果找到,它将返回到终点的完整最短路径。

import java.util.*;

public class Maze {

  public static int[][] arr = new int[][] {
            {0,0,0,0,0,0,0,0,0},
            {5,5,5,0,0,0,0,0,0},
            {0,0,0,5,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,0},
            {0,0,0,0,0,0,0,0,9},
    };

  private static class Point {
        int x;
        int y;
        Point parent;

        public Point(int x, int y, Point parent) {
            this.x = x;
            this.y = y;
            this.parent = parent;
        }

        public Point getParent() {
            return this.parent;
        }

        public String toString() {
            return "x = " + x + " y = " + y;
        }
  }

  public static Queue<Point> q = new LinkedList<Point>();

    public static Point getPathBFS(int x, int y) {

        q.add(new Point(x,y, null));

        while(!q.isEmpty()) {
            Point p = q.remove();

            if (arr[p.x][p.y] == 9) {
                System.out.println("Exit is reached!");
                return p;
            }

            if(isFree(p.x+1,p.y)) {
                arr[p.x][p.y] = -1;
                Point nextP = new Point(p.x+1,p.y, p);
                q.add(nextP);
            }

            if(isFree(p.x-1,p.y)) {
                arr[p.x][p.y] = -1;
                Point nextP = new Point(p.x-1,p.y, p);
                q.add(nextP);
            }

            if(isFree(p.x,p.y+1)) {
                arr[p.x][p.y] = -1;
                Point nextP = new Point(p.x,p.y+1, p);
                q.add(nextP);
            }

             if(isFree(p.x,p.y-1)) {
                arr[p.x][p.y] = -1;
                Point nextP = new Point(p.x,p.y-1, p);
                q.add(nextP);
            }

        }
        return null;
    }


    public static boolean isFree(int x, int y) {
        if((x >= 0 && x < arr.length) && (y >= 0 && y < arr[x].length) && (arr[x][y] == 0 || arr[x][y] == 9)) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {

        Point p = getPathBFS(0,0);

         for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }

        while(p.getParent() != null) {
            System.out.println(p);
            p = p.getParent();
        }

    }

}

答案 1 :(得分:5)

简答:是的

说明:

伪代码表示通过迷宫的路径作为树叶的路径。迷宫中的每个点都是树上的一个节点,你可以从那里去的每个新点都是该节点的子节点。

为了进行广度优先搜索,算法首先必须考虑通过长度为1的树,然后是长度为2的树的所有路径,直到它到达结尾,这将导致算法停止,因为结束没有孩子,导致空队列。

代码使用队列(Q)跟踪它需要访问的节点。它首先将迷宫的起点设置为树的根,访问它(检查它是否是结束),然后从队列中删除根并为每个子节点重复该过程。通过这种方式,它按顺序访问节点,即root,(root的每个子节点),(第一个孩子的每个孩子),(第二个孩子的每个孩子)等,直到它结束。

编辑:就目前而言,算法在到达末尾时可能不会因为队列中的其他节点而终止。你必须自己做好终止条件。