There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.

Given the ball's start position, the destination and the maze, find the shortest distance for the ball to stop at the destination. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the destination (included). If the ball cannot stop at the destination, return -1.

The maze is represented by a binary 2D array. 1 means the wall and 0 means the empty space. You may assume that the borders of the maze are all walls. The start and destination coordinates are represented by row and column indexes.

 class Solution {
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int row = maze.length;
int col = maze[0].length;
int res = 0;
Queue<Cell> queue = new LinkedList<>();
int[][] dists = new int[row][col];
for (int[] dist: dists) {
Arrays.fill(dist, -1);
}
queue.offer(new Cell(start[0], start[1]));
dists[start[0]][start[1]] = 0;
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
int size = queue.size();
Cell cur = queue.poll();
int curX = cur.x;
int curY = cur.y; for (int[] direction: directions) {
int newX = curX;
int newY = curY;
int curDist = dists[curX][curY]; while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
newX += direction[0];
newY += direction[1];
curDist += 1;
}
newX -= direction[0];
newY -= direction[1];
curDist -= 1;
if (dists[newX][newY] == -1 || curDist < dists[newX][newY]) {
dists[newX][newY] = curDist;
queue.offer(new Cell(newX, newY));
}
} }
return dists[destination[0]][destination[1]];
}
} class Cell {
int x;
int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
}

[LC] 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. 505. The Maze II

    原题链接:https://leetcode.com/articles/the-maze-ii/ 我的思路 在做完了第一道迷宫问题 http://www.cnblogs.com/optor/p/8533 ...

  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. LC 499. The Maze III 【lock,hard】

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

随机推荐

  1. JavaWeb之Servlet入门(一)

    1. Servlet介绍 Servlet(Server Applet),全称Java Servlet,是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 2. ...

  2. OpenMP笔记(四)

    个人博客地址:http://www.bearoom.xyz/2019/02/22/openmp4/ 一.private private子句用于将一个或多个变量声明成线程私有的变量,这样每个线程都有该变 ...

  3. 当初希望自己是如何投入这个专业的学习的?曾经做过什么准备,或者立下过什么FLAG吗?

    学习好累,打游戏好爽  我不爱学习 认真勤勉投入学习 精心准备,刻苦学习 我的flag   作为大学生,需要了解今后职场社会,对职业方向有了进一步的认识.社会对于人才的要求在某些方面都是不谋而合的,比 ...

  4. linux的vi编辑器中如何查找内容(关键字)

    按下”/“键,这时在状态栏(也就是屏幕左下脚)就出现了 “/” 然后输入你要查找的关键字敲回车就可以了. 找到相关文字以后: (1)按下小写n,向下查找 (2)按下大写N,向上查找

  5. 计算机网络(2): http的基础上用SSL或TSL加密

    加密过程具体TCP实现 步骤 1 : 客户端通过发送Client Hello报文开始SSL通信(这里是在TCP的三次握手已经完成的基础上进行的).报文中包含客户端支持的SSL的指定版本.加密组件列表( ...

  6. 3分钟搞定高逼格PPT封底——简约型

    封底想要高逼格又简约? 发现了这五类,看完不会制作算我输.   一.纯文字 白色背景下,一段结束语,或提问或感谢.       重叠文字,看上去非常有创意. 没有操作难度,END放大字号,颜色设置为浅 ...

  7. 深入分析Java反射(六)-反射调用异常处理

    前提 Java反射的API在JavaSE1.7的时候已经基本完善,但是本文编写的时候使用的是Oracle JDK11,因为JDK11对于sun包下的源码也上传了,可以直接通过IDE查看对应的源码和进行 ...

  8. aop 实现原理

    aop 底层采用代理机制实现 接口 + 实现类 :spring 采用 jdk 的 动态代理 只有实现类:spring 采用 cglib 字节码增强 aop专业术语 1.target(目标) 需要被代理 ...

  9. js字符串相关要点

    不要创建string对象,它会拖慢执行速度,并可能产生其他副作用. var x = "John"; var y = new String("John"); (x ...

  10. 架构之道(3) - 令後端的吐血和喊FUCK的次数锐减

    「那个产品经理不会技术,整天在需求,真操他妈的.」 这是很多产品经理遇到的一句话,如果你把顾客阶段完成了,回到自己的团队,遇到个技术大牛这麽说,那就表示,自己作为产品经理的功力还不够. 等我慢现解释, ...