# [Java编程]迷宫问题（BFS）

``````import java.util.*;

public class Main {
public static void main(String[] args) {
int[][] tab = {
{0, 1, 0, 0, 0},
{0, 1, 0, 1, 0},
{0, 0, 0, 1, 0},
{0, 1, 1, 1, 0},
{0, 0, 0, 0, 0}
};
bfs(tab);
}

public static void bfs(int[][] tab) {
//记录走到该坐标需要的最短步数
int[][] distance = new int[5][5];
//记录移动的四个方位
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
while(!queue.isEmpty()) {
Point p = queue.poll();
//到达终点
if(p.getX() == 4 && p.getY() == 4)
break;
for(int i = 0; i < 4; i++) {
int x = p.getX() + dx[i];
int y = p.getY() + dy[i];
//越界判断，障碍判断，是否走过判断
if(x >= 0 && y >= 0 && x < 5 && y < 5
&& tab[x][y] != 1 && distance[x][y] == 0) {
distance[x][y] = distance[p.getX()][p.getY()] + 1;
}
}
}
System.out.println(distance[4][4]);
}

}

//点类，用于存储坐标(x,y)
class Point {
int x;
int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
}
``````
原文作者：BFS
原文地址: https://blog.csdn.net/weixin_38278878/article/details/81607565
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。