[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 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的更多相关文章
- [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 ...
- LeetCode 505. The Maze II
原题链接在这里:https://leetcode.com/problems/the-maze-ii/ 题目: There is a ball in a maze with empty spaces a ...
- [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 ...
- 505. The Maze II
原题链接:https://leetcode.com/articles/the-maze-ii/ 我的思路 在做完了第一道迷宫问题 http://www.cnblogs.com/optor/p/8533 ...
- 【LeetCode】505. The Maze II 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- A Dangerous Maze (II) LightOJ - 1395(概率dp)
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
- LightOJ - 1395 A Dangerous Maze (II) —— 期望
题目链接:https://vjudge.net/problem/LightOJ-1395 1395 - A Dangerous Maze (II) PDF (English) Statistic ...
- lintcode 787. The Maze 、788. The Maze II 、
787. The Maze https://www.cnblogs.com/grandyang/p/6381458.html 与number of island不一样,递归的函数返回值是bool,不是 ...
- 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 ...
随机推荐
- 解决Android Studio的安装问题
今天开始了android studio的下载与安装,我再官网上下载了Android studio,下载不难,运行出来可需要一定的时间,在中途中我遇到了一些问题 一:Build错误: 在我最开始下载完A ...
- 动态类型识别&动态创建
以下大部分内容摘自<windows程序设计 第2版> 王艳平 张铮 编著 动态类型识别:在程序运行过程中,辨别对象是否属于特定类的技术. 应用举例:函数辨别参数类型.需要针对对象的类编写特 ...
- [C/C++]C/C++计算代码的运行时间
有很多时候,实现一个功能后可能不仅仅要效果,还要效率,如果可以在极短的时间内完成一个功能那当然是最好不过的啦,但是可能经常会事与愿违. 这里就写一下,都可以怎样用C/C++或者Qt的方法来测试代码的运 ...
- JQuery与JS比较
备注:原文转自脚本之家,原文地址:http://www.jb51.net/article/39494.htm 转载仅为方便统一整理收藏内容,别无他意 JQuery与JS的比较: jquery ...
- 如何正确理解SQL关联子查询
一.基本逻辑 对于外部查询返回的每一行数据,内部查询都要执行一次.在关联子查询中是信息流是双向的.外部查询的每行数据传递一个值给子查询,然后子查询为每一行数据执行一次并返回它的记录.然后,外部查询根据 ...
- polyA|ribo-minus|differentiated cell|Genetic heterogeneity
转录组 测量单cell,可以认为是一种细胞.细胞株也认为来自同一个细胞. 使用两种方法,找mRNA(polyA)及rmRNA(ribo-minus),然后取交集. 转录组受实验影响,比如小片段没得到. ...
- sol - 0x60,61,62
[例题]走廊泼水节 设当前扫描到边x,y,长度为z,x所处的并查集为Sx,y所处的并查集为Sy: 对于任意u属于Sx,v属于Sy,我们可以知道u,v之间必连一条边 但是我们要在保证x,y之间的边属于唯 ...
- 追踪tracking
追踪 追踪部分的主要思路是在当前帧和(局部)地图之间寻找尽可能多的对应关系,来优化当前帧的位姿. 作者在追踪这部分主要用了几种模型:运动模型(Tracking with motion model).关 ...
- 树莓派docker搭建
树莓派上 Docker 的安装和使用 Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟 ...
- AFNetWorking3.0.4(Weather Demo)
参考文章:http://www.jianshu.com/p/99bcc40abb30 demo下载地址:http://pan.baidu.com/s/1eRuYDEi 密码:ma5m 使用最新AFNe ...