Leetcode: The Maze II

时间:2022-02-19 15:09:46
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.

Example 1

Input 1: a maze represented by a 2D array

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 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (4, 4) Output: 12
Explanation: One shortest way is : left -> down -> left -> down -> right -> down -> right.
The total distance is 1 + 1 + 3 + 1 + 2 + 2 + 2 = 12.
Leetcode: The Maze II
Example 2 Input 1: a maze represented by a 2D array 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 Input 2: start coordinate (rowStart, colStart) = (0, 4)
Input 3: destination coordinate (rowDest, colDest) = (3, 2) Output: -1
Explanation: There is no way for the ball to stop at the destination.
Leetcode: The Maze II Note:
There is only one ball and one destination in the maze.
Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
The given maze does not contain border (like the red rectangle in the example pictures), but you could assume the border of the maze are all walls.
The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

Solution of The Mazehttps://discuss.leetcode.com/topic/77471/easy-understanding-java-bfs-solution
Solution of The Maze IIIhttps://discuss.leetcode.com/topic/77474/similar-to-the-maze-ii-easy-understanding-java-bfs-solution

We need to use PriorityQueue instead of standard queue, and record the minimal length of each point.

 public class Solution {
class Point {
int x,y,l;
public Point(int _x, int _y, int _l) {x=_x;y=_y;l=_l;}
}
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
int m=maze.length, n=maze[0].length;
int[][] length=new int[m][n]; // record length
for (int i=0;i<m*n;i++) length[i/n][i%n]=Integer.MAX_VALUE;
int[][] dir=new int[][] {{-1,0},{0,1},{1,0},{0,-1}};
PriorityQueue<Point> list=new PriorityQueue<>((o1,o2)->o1.l-o2.l); // using priority queue
list.offer(new Point(start[0], start[1], 0));
while (!list.isEmpty()) {
Point p=list.poll();
if (length[p.x][p.y]<=p.l) continue; // if we have already found a route shorter
length[p.x][p.y]=p.l;
for (int i=0;i<4;i++) {
int xx=p.x, yy=p.y, l=p.l;
while (xx>=0 && xx<m && yy>=0 && yy<n && maze[xx][yy]==0) {
xx+=dir[i][0];
yy+=dir[i][1];
l++;
}
xx-=dir[i][0];
yy-=dir[i][1];
l--;
list.offer(new Point(xx, yy, l));
}
}
return length[destination[0]][destination[1]]==Integer.MAX_VALUE?-1:length[destination[0]][destination[1]];
}
}

Leetcode: The Maze II的更多相关文章

  1. &lbrack;LeetCode&rsqb; 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. &lbrack;LeetCode&rsqb; The Maze III 迷宫之三

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

  3. &lbrack;LeetCode&rsqb; The Maze 迷宫

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

  4. &lbrack;LeetCode&rsqb; Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  5. &lbrack;leetcode&rsqb;Word Ladder II &commat; Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  6. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  7. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  8. A Dangerous Maze &lpar;II&rpar; LightOJ - 1395&lpar;概率dp&rpar;

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

  9. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

随机推荐

  1. 为Linux重新开发MVC&comma;有图有真相

    1.写在前面 就连我们自己开始时也在问自己:我们为什么要开发一套MVC,微软的难道不可用用吗? 一开始的理由很简单.为了更好地跨平台部署;在Linux部署过.NET的人们应该知道, 部署起来是有点繁琐 ...

  2. C&num; 解析JSON格式数据

    JSON简介 JSON(全称为JavaScript ObjectNotation) 是一种轻量级的数据交换格式.它是基于JavaScript语法标准的一个子集.JSON采用完全独立于语言的文本格式,可 ...

  3. 【java】 java 实现mysql备份

    使用java实现mysql的备份: public class MySQLBackUp { /** * Java代码实现MySQL数据库导出 * * @author GaoHuanjie * @para ...

  4. 远程连接腾讯云服务器MySQL数据库

    1.添加腾讯云安全组规则的MySQL 3306端口 将所有端口打开,至少打开3306,不在赘述. 2.打开更改MySQL配置文件 打开配置文件 vi /etc/mysql/mysql.conf.d/m ...

  5. SAS 分组与排序

    SAS 分组与排序 SAS对数据集进行操作时,经常需要在SET.MERGE.MODIFY或 UPDATE语句中使用分组数据.使用分组数据最基本的方法是使用BY 语句,其基本形式如下: BY 变量列表; ...

  6. BZOJ2839 &colon; 集合计数 &lpar;广义容斥定理&rpar;

    题目 一个有 \(N\) 个 元素的集合有 \(2^N\) 个不同子集(包含空集), 现在要在这 \(2^N\) 个集合中取出若干集合(至少一个), 使得它们的交集的元素个数为 \(K\) ,求取法的 ...

  7. touch修改文件时间戳

    https://blog.csdn.net/lsbhjshyn/article/details/51443304 touch -t 20181011000.01 text.txt

  8. 简单线性回归问题的优化(SGD)R语言

    本编博客继续分享简单的机器学习的R语言实现. 今天是关于简单的线性回归方程问题的优化问题 常用方法,我们会考虑随机梯度递降,好处是,我们不需要遍历数据集中的所有元素,这样可以大幅度的减少运算量. 具体 ...

  9. vs 15 key

    vs 15 Key :HM6NR-QXX7C-DFW2Y-8B82K-WTYJV vs 15 Key :2XNFG-KFHR8-QV3CP-3W6HT-683CH

  10. &lpar;一&rpar;SpringMVC

    第一章 问候SpringMVC 第一节 SpringMVC简介 SpringMVC是一套功能强大,性能强悍,使用方便的优秀的MVC框架 下载和安装Spring框架: 登录http://repo.spr ...