利用广度优先遍历算法的特点,由于迷宫每次只能走一格,所以对于任意一个节点,bfs第一次到达该点时一定是最短路径。
package recursion;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Stack;
public class Maze {
private class Node{
int x;
int y;
public Node(int x, int y) {
this.x = x;
this.y = y;
}
}
private int[][] map;
//起点
private int startX;
private int startY;
//终点
private int endX;
private int endY;
//代表上下左右四个可能走的方向
private int[] dx = {
1,0,-1,0};
private int[] dy = {
0,1,0,-1};
public Maze(int[][] map, int startX, int startY, int endX, int endY) {
this.map = map;
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
}
public void print(int[]