300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > Java与算法之(5) - 老鼠走迷宫(深度优先算法)

Java与算法之(5) - 老鼠走迷宫(深度优先算法)

时间:2018-10-30 19:18:36

相关推荐

Java与算法之(5) - 老鼠走迷宫(深度优先算法)

小老鼠走进了格子迷宫,如何能绕过猫并以最短的路线吃到奶酪呢?

注意只能上下左右移动,不能斜着移动。

在解决迷宫问题上,深度优先算法的思路是沿着一条路一直走,遇到障碍或走出边界再返回尝试别的路径。

首先用一个二维数组来把迷宫“数字化”。

int[][] maze = new int[5][4];

迷宫中每个格子的横纵坐标对应数组的一维和二维索引,例如最左上角的格子是maze[0][0],数组的值表示该格子是否可以通过,0表示可以通过,1表示该格子有猫。

初始化迷宫,标记猫的位置:

this.maze[2][0] = 1;this.maze[1][2] = 1;this.maze[2][2] = 1;this.maze[3][2] = 1;

起点位置坐标是x=0,y=0,如果向右移动就是x=x+1,y=y,向下移动是x=x,y=y+1。我们预先规定每到一个格子都按照右、下、左、上的顺序尝试下一个格子是否能走,如果右边的格子没有猫且未出边界,就移动到下一个格子,继续按照右、下、左、上的顺序尝试;如果右边的格子不能走则尝试下面的格子。

下面这个二维数组用来遍历尝试四个方向的格子:

int[][] next = new int[][] {{1, 0},{0, 1},{-1, 0},{0, -1}};

为了不走回头路,我们还需要另外一个二维数组标记哪些格子是已走过的,如果已走过则不能回头。

int[][] mark = new int[5][4];

用一个栈记录路径

LinkedList<Integer> map = new LinkedList<>();

走格子的思路是:

for(遍历四个方向的格子) {if(格子超出边界 或 格子有猫 或 格子已经走过) {continue;} else {移动到格子记录当前格子已走过记录当前路径for(以新格子为中心遍历四个方向的格子) {......}}}

但是我们并不知道要走多少步才能到达目标,也就不知道循环要嵌套多少层,但是可以看出每次新的遍历循环开启后,执行的代码和上一层循环是一样的,所以这里用递归解决。来看完整的代码:

import java.util.LinkedList;public class DfsRatMaze {int min = Integer.MAX_VALUE;int endX = 3; //目标点横坐标int endY = 3; //目标点纵坐标int width = 5; //迷宫宽度int height = 4; //迷宫高度int[][] maze = new int[5][4];int[][] mark = new int[5][4];LinkedList<Integer> map = new LinkedList<>();public void dfs(int startX, int startY, int step) {int[][] next = new int[][] { //按右->下->左->上的顺序尝试{1, 0},{0, 1},{-1, 0},{0, -1}};int nextX, nextY;int posible;if(startX == endX && startY == endY) {if(step < min)min = step;for(int i = map.size() - 1; i >= 0; i -= 2){nextX = map.get(i);nextY = map.get(i - 1);System.out.print("[" + nextX + "," + nextY + "]");if(i != 1)System.out.print("->");}System.out.println();return;}for(posible = 0; posible < next.length; posible++) { //按右->下->左->上的顺序尝试nextX = startX + next[posible][0];nextY = startY + next[posible][1];if(nextX < 0 || nextX >= width || nextY < 0 || nextY >= height) { //超出边界continue;}if(maze[nextX][nextY] == 0 && mark[nextX][nextY] == 0) { //非障碍且未标记走过map.push(nextX);map.push(nextY);mark[nextX][nextY] = 1;dfs(nextX, nextY, step + 1); //递归调用, 移动到下一格mark[nextX][nextY] = 0;map.pop();map.pop();}}}/** 初始化迷宫*/public void initMaze() {this.maze = new int[width][height];this.mark = new int[width][height];this.maze[2][0] = 1;this.maze[1][2] = 1;this.maze[2][2] = 1;this.maze[3][2] = 1;this.mark[0][0] = 1;//打印迷宫 _表示可通行 *表示障碍 !表示目标for(int y = 0; y < height; y++) {for(int x = 0; x < width; x++) {if(x == endX && y == endY) {System.out.print("! ");} else if(this.maze[x][y] == 1) {System.out.print("* ");} else {System.out.print("_ ");}}System.out.println();}System.out.println();}public static void main(String[] args) {int startX = 0;int startY = 0;DfsRatMaze d = new DfsRatMaze();d.initMaze();d.dfs(startX, startY, 0);if(d.min < Integer.MAX_VALUE)System.out.println("最少需要" + d.min + "步");elseSystem.out.println("目标地点无法到达");}}

运行后输出:

[1,0]->[1,1]->[2,1]->[3,1]->[4,1]->[4,2]->[4,3]->[3,3][1,0]->[1,1]->[2,1]->[3,1]->[3,0]->[4,0]->[4,1]->[4,2]->[4,3]->[3,3][1,0]->[1,1]->[0,1]->[0,2]->[0,3]->[1,3]->[2,3]->[3,3][0,1]->[1,1]->[2,1]->[3,1]->[4,1]->[4,2]->[4,3]->[3,3][0,1]->[1,1]->[2,1]->[3,1]->[3,0]->[4,0]->[4,1]->[4,2]->[4,3]->[3,3][0,1]->[0,2]->[0,3]->[1,3]->[2,3]->[3,3]最少需要6步

可以看到,程序计算出了所有路线,并找到了最短的路线。而整个代码还不到100行,真是神奇的算法。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。