算法 迷宫算法总结(最短路径)
@author:Jingdai
@date:2020.11.20
迷宫问题是一个非常经典的算法问题,现总结一下。
题目描述
给你一个迷宫,并给你一个起点和终点,请你给出从起点到终点的最短路径,若不存在最短路径,输出 -1。迷宫中用
'#'
代表障碍,其余字符都可以走。输入描述,第一行两个数代表迷宫的大小(行数m和列数n);第二行代表起点和终点的坐标;后面的 m 行是迷宫。
输入示例:
10 10 0 1 9 8 #S######.# ......#..# .#.##.##.# .#........ ##.##.#### ....#....# .#######.# ....#..... .####.###. ....#...G#
输出示例:
22
思路
对于迷宫问题,可以使用 dfs ,也可以使用 bfs,但是题目要求是最短路径,如果使用 dfs,需要遍历所有的情况,当找到一个路径时不能直接返回,需要记录当前路径并继续寻找下一条路径,然后从所有路径中算出最短的,可以看出 dfs 算最短路径比较麻烦,故这里使用 bfs。
使用一个
distance
数组记录遍历到的点与起点的距离,distance
数组中起点距离初始化为0,其余点均初始化为 int 的最大值INT_MAXVALUE
。然后进行 bfs,用一个队列queue
记录遍历到的点,并将其在distance
中的值记为它的前一个点的值加一。遍历过程中,除了障碍点不能走外,遍历过的点也不能走(遍历过说明之前有更短的路径到该点,所以现在一定不是最短路径)。那怎么判断是否遍历过呢,很简单,最开始我们初始化为
INT_MAXVALUE
,如果它的值是INT_MAXVALUE
,代表没遍历过,否则代表遍历过。当遍历到终点时,代表找到,直接返回。如果遍历到最后都不能到达终点,说明不可达,返回 -1。下面是代码。
import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int row; int column; row = scanner.nextInt(); column = scanner.nextInt(); scanner.nextLine(); int[] start = new int[2]; int[] end = new int[2]; start[0] = scanner.nextInt(); start[1] = scanner.nextInt(); end[0] = scanner.nextInt(); end[1] = scanner.nextInt(); scanner.nextLine(); char[][] maze = new char[row][column]; for (int i = 0; i < row; i++) { String curRow = scanner.nextLine(); maze[i] = curRow.toCharArray(); } System.out.println(bfsPath(maze, start, end)); } public static int bfsPath(char[][] maze, int[] start, int[] end) { int row = maze.length; int column = maze[0].length; int[][] distance = new int[row][column]; for (int i = 0; i < row; i++) { Arrays.fill(distance[i], Integer.MAX_VALUE); } int[][] direction = { { 0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; // initiate LinkedList<int[]> queue = new LinkedList<>(); queue.offer(start); distance[start[0]][start[1]] = 0; boolean isFound = false; while (queue.size() != 0) { int[] pre = queue.poll(); for (int i = 0; i < 4; i++) { int[] cur = { pre[0] + direction[i][0], pre[1] + direction[i][1]}; if (cur[0] >= 0 && cur[0] < row && cur[1] >= 0 && cur[1] < column && maze[cur[0]][cur[1]] != '#' && distance[cur[0]][cur[1]] == Integer.MAX_VALUE) { // can go distance[cur[0]][cur[1]] = distance[pre[0]][pre[1]] + 1; if (cur[0] == end[0] && cur[1] == end[1]) { // find isFound = true; break; } queue.offer(cur); } } if (isFound) { break; } } return distance[end[0]][end[1]] == Integer.MAX_VALUE ? -1 : distance[end[0]][end[1]]; } }
这里只是输出了最短路径的长度,如果题目是求具体的最短路径呢?其实也很容易,利用我们之前的
distance
数组,从终点开始,往回找。若点a是路径中的一个点,那它前一个点是哪一个呢?就是它周围那些点中distance
距离等于它减1的那个点,根据此就可以往回一步一步找到路径。因为是倒序,所以这里利用栈后进先出的性质完成路径的正序输出,代码如下。import java.util.*; public class Solution { public static int[][] distance; public static int row; public static int column; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); row = scanner.nextInt(); column = scanner.nextInt(); scanner.nextLine(); int[] start = new int[2]; int[] end = new int[2]; start[0] = scanner.nextInt(); start[1] = scanner.nextInt(); end[0] = scanner.nextInt(); end[1] = scanner.nextInt(); scanner.nextLine(); char[][] maze = new char[row][column]; for (int i = 0; i < row; i++) { String curRow = scanner.nextLine(); maze[i] = curRow.toCharArray(); } distance = new int[row][column]; for (int i = 0; i < row; i++) { Arrays.fill(distance[i], Integer.MAX_VALUE); } int pathLength = bfsPath(maze, start, end); System.out.println(pathLength); LinkedList<int[]> path = null; if (pathLength != -1) { path = generatePath(start, end); for (int[] step : path) { System.out.println(Arrays.toString(step)); } } } public static int bfsPath(char[][] maze, int[] start, int[] end) { int row = maze.length; int column = maze[0].length; int[][] direction = { { 0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; // initiate LinkedList<int[]> queue = new LinkedList<>(); queue.offer(start); distance[start[0]][start[1]] = 0; boolean isFound = false; while (queue.size() != 0) { int[] pre = queue.poll(); for (int i = 0; i < 4; i++) { int[] cur = { pre[0] + direction[i][0], pre[1] + direction[i][1]}; if (cur[0] >= 0 && cur[0] < row && cur[1] >= 0 && cur[1] < column && maze[cur[0]][cur[1]] != '#' && distance[cur[0]][cur[1]] == Integer.MAX_VALUE) { // can go distance[cur[0]][cur[1]] = distance[pre[0]][pre[1]] + 1; if (cur[0] == end[0] && cur[1] == end[1]) { // find isFound = true; break; } queue.offer(cur); } } if (isFound) { break; } } return distance[end[0]][end[1]] == Integer.MAX_VALUE ? -1 : distance[end[0]][end[1]]; } public static LinkedList<int[]> generatePath(int[] start, int[] end) { LinkedList<int[]> path = new LinkedList<>(); int[] cur = new int[]{ end[0], end[1]}; int[][] direction = { { 0, 1}, { 1, 0}, { 0, -1}, { -1, 0}}; while (true) { path.push(cur); if (cur[0] == start[0] && cur[1] == start[1]) { break; } for (int i = 0; i < 4; i++) { if (cur[0] + direction[i][0] >= 0 && cur[0] + direction[i][0] < row && cur[1]+direction[i][1] >= 0 && cur[1]+direction[i][1] < column) { if (distance[cur[0] + direction[i][0]][cur[1] + direction[i][1]] == distance[cur[0]][cur[1]] - 1) { cur = new int[]{ cur[0] + direction[i][0], cur[1] + direction[i][1]}; break; } } } } return path; } }