我定义了一个结构体,记录坐标和当前的路径,队列存储结构体类型。
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
public class GoMaze {
//走迷宫问题
//经典的bfs模板记录路径题
//(0, 0)->(1,0)->(2,0)->(2,1)->(2,2)->(2,3)->(2,4)->(3,4)->(4,4)
static class Node{
int x;
int y;
String sb;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
//设置方向数组
static int [][] dir={{-1,0},{0,1},{1,0},{0,-1}};
//设置标记数组避免走回头路
static boolean [][] visited;
//最短路径集合
static ArrayList res=new ArrayList();
public static void main(String[] args) {
//初始化迷宫图案(Initializes the maze pattern)
int [][] maze = {
{0, 0, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 0, 1},
{0, 1, 1, 0, 0},
{0, 1, 0, 1, 0}
};
bfs(maze);
//这个版本只能记录一条最短路径(思考:假如要记录所有的最短路径该如何修改)
System.out.println(res);
}
public static void bfs(int [][] maze){
int m=maze.length,n=maze[0].length;
visited=new boolean[m][n];
Deque<Node> q=new ArrayDeque<Node>();
Node p=new Node(0,0);
p.sb="("+p.x+","+p.y+")";
q.add(p);
visited[p.x][p.y]=true;
//标准的bfs框架相当于N叉树的遍历
while (!q.isEmpty()){
Node cur=q.poll();
if(cur.x==m-1&&cur.y==n-1){
res.add(cur.sb);
}
for(int i=0;i<4;i++){
int nx= cur.x+dir[i][0];
int ny= cur.y+dir[i][1];
if(nx>=0&&nx<m&&ny>=0&&ny<n&&!visited[nx][ny]&&maze[nx][ny]==0){
Node nextp=new Node(nx,ny);
nextp.sb=cur.sb+"->("+nextp.x+","+nextp.y+")";
q.add(nextp);
visited[nx][ny]=true;
}
}
}
}
}