原题链接:https://leetcode.com/articles/the-maze-ii/

我的思路

在做完了第一道迷宫问题 http://www.cnblogs.com/optor/p/8533068.html 后,这第二道迷宫问题就比较简单了。

题意是求最短路径,所以我觉得使用深度优先搜索不合适(因为深度优先搜索需要遍历完所有走法之后再取路径最短的,比较麻烦),而广度优先搜索则较为适合这个问题。所以我尝试写了下广度优先搜索的实现:

import java.util.LinkedList;
import java.util.Queue; /**
* Created by clearbug on 2018/2/26.
*/
public class Solution { public static void main(String[] args) {
Solution s = new Solution();
/**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0
*/
int[][] board = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.hasPath(board, new int[]{0, 4}, new int[]{4, 4})); /**
* 0 0 1 0 0
0 0 0 0 0
0 0 0 1 0
1 1 0 1 1
0 0 0 0 0 */
int[][] board2 = {
{0, 0, 1, 0, 0},
{0, 0, 0, 0, 0},
{0, 0, 0, 1, 0},
{1, 1, 0, 1, 1},
{0, 0, 0, 0, 0},
};
System.out.println(s.hasPath(board2, new int[]{0, 4}, new int[]{3, 2}));
} public int hasPath(int[][] maze, int[] start, int[] dest) {
maze[start[0]][start[1]] = 2; int[][] dirs = {
{0, 1},
{0, -1},
{-1, 0},
{1, 0}
}; Queue<int[]> queue = new LinkedList<>();
queue.add(start); while (!queue.isEmpty()) {
int[] s = queue.remove();
if (s[0] == dest[0] && s[1] == dest[1]) {
return maze[s[0]][s[1]] - 2;
}
for (int[] dir : dirs) {
int x = s[0] + dir[0];
int y = s[1] + dir[1];
while (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] != 1) {
x += dir[0];
y += dir[1];
}
if (maze[x - dir[0]][y - dir[1]] == 0) {
queue.add(new int[]{x - dir[0], y - dir[1]});
maze[x - dir[0]][y - dir[1]] = maze[s[0]][s[1]] + Math.abs(x - dir[0] - s[0]) + Math.abs(y - dir[1] - s[1]);
}
}
} return -1;
} }

直接在上一题的广度优先搜索算法实现上修改就行啦!!!下面去看看官方的解法是怎样的吧!

官方方法一:深度优先搜索

这次就不抄代码了,只想说官方提供的答案就是思路清晰,代码简介!

官方方法二:广度优先搜索

感觉这里的广度优先搜索算法实现里面稍微有点不妥啊,貌似还不如我的实现呢哈哈

官方方法三:使用迪杰斯特拉算法

把求图的最短距离的迪杰斯特拉算法用在了这里,迪杰斯特拉算法我也是看了大半天才看懂了。官方的实现又是看了半天看懂了,就不写了,有点复杂啊!

学习迪杰斯特拉算法:https://www.youtube.com/watch?v=F728NKEeODQ

官方方法四:迪杰斯特拉算法+优先级队列

在方法三的基础上使用优先级队列进行了优化,迪杰斯特拉算法对于目前的我来说就够复杂了,再加上优先级队列。。。代码实现基本上是看懂了,所以先这样吧!

505. The Maze II的更多相关文章

  1. [LeetCode] 505. The Maze II 迷宫之二

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  2. LeetCode 505. The Maze II

    原题链接在这里:https://leetcode.com/problems/the-maze-ii/ 题目: There is a ball in a maze with empty spaces a ...

  3. [LeetCode] 505. The Maze II 迷宫 II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  4. [LC] 505. The Maze II

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  5. 【LeetCode】505. The Maze II 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  6. A Dangerous Maze (II) LightOJ - 1395(概率dp)

    A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...

  7. LightOJ - 1395 A Dangerous Maze (II) —— 期望

    题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II)    PDF (English) Statistic ...

  8. lintcode 787. The Maze 、788. The Maze II 、

    787. The Maze https://www.cnblogs.com/grandyang/p/6381458.html 与number of island不一样,递归的函数返回值是bool,不是 ...

  9. [LeetCode] The Maze II 迷宫之二

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

随机推荐

  1. 盘点Mysql的登陆方式

    前置知识 我们想登陆到mysql中前提是肯定需要一个用户名和密码:比如 root root 在mysql中用户的信息会存放在 mysql数据库下的 user表中 可以 use mysql 然后sele ...

  2. NetCore项目实战篇08---Docker挂载mysql并连接.netCoreWeb

    我们的项目之前在直接连接的mysql,今天我们将通过docker挂载mysql 并与我们开发的webapi项目连接. 1. 安装docker 下载地址: https://download.docker ...

  3. 这个Maven依赖的问题,你敢说你没遇到过

    Maven 依赖没处理好的话经常会导致发生一些问题,非常烦.今天给大家分享一个依赖相关的问题,说不定你之前就遇到过. 问题背景 有个 ES 搜索的项目,刚开始还是好好的状态,过了一段时间,然后就发现启 ...

  4. Chisel3 - bind - Data

    https://mp.weixin.qq.com/s/ENJVkz88sGgyODRNCu9jhQ   介绍Data类中的binding的定义和用法.   Binding stores informa ...

  5. java实现串中找数字

    串中找数字 以下的静态方法实现了:把串s中第一个出现的数字的值返回. 如果找不到数字,返回-1 例如: s = "abc24us43" 则返回2 s = "82445ad ...

  6. Java实现第八届蓝桥杯字母组串

    字母组串 由 A,B,C 这3个字母就可以组成许多串. 比如:"A","AB","ABC","ABA","AA ...

  7. jstat监控JVM内存使用、GC回收情况

    jstat -gcutil 2388 3000 6 每隔3秒打印一次pid为2388的堆内存的使用情况,共打印6次 S0— Heap上的 Survivor space 0 区已使用空间的百分比 S1  ...

  8. zabbix服务无法正常启动

    环境介绍 操作系统 centos 7.4 zabbix 3.4.7   故障现象说明 zabbix agent无法启动   web 监控不到客户端服务器   查看日志报错 提示不能打开日志,不能创建信 ...

  9. Spring Security 实战干货:如何实现不同的接口不同的安全策略

    1. 前言 欢迎阅读 Spring Security 实战干货 系列文章 .最近有开发小伙伴提了一个有趣的问题.他正在做一个项目,涉及两种风格,一种是给小程序出接口,安全上使用无状态的JWT Toke ...

  10. test for OCr