Leetcode: 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. 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.
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.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 Maze: https://discuss.leetcode.com/topic/77471/easy-understanding-java-bfs-solution
Solution of The Maze III: https://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的更多相关文章
- [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 ...
 - [LeetCode] The Maze III 迷宫之三
		
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
 - [LeetCode] The Maze 迷宫
		
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
 - [LeetCode] Palindrome Partitioning II 解题笔记
		
Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...
 - [leetcode]Word Ladder II @ Python
		
[leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...
 - LeetCode:课程表II【210】
		
LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...
 - LeetCode:全排列II【47】
		
LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...
 - A Dangerous Maze (II) LightOJ - 1395(概率dp)
		
A Dangerous Maze (II) LightOJ - 1395(概率dp) 这题是Light Oj 1027的加强版,1027那道是无记忆的. 题意: 有n扇门,每次你可以选择其中一扇.xi ...
 - LeetCode:子集 II【90】
		
LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...
 
随机推荐
- String Formatting in C#
			
原文地址 http://blog.stevex.net/string-formatting-in-csharp/ When I started working with the .NET framew ...
 - 一、ESP8266入门(基于LUA开发)
			
序 一入坑便停不下来... 还挺有意思的哈,233,,,, 资料杂,自己一个一个去找确实浪费了不少时间,而且大多还都是英文的,需要硬着头皮看. 这次实践入门,更是对英语的重要确信无疑.Github必须 ...
 - Vue学习陷阱
			
v-for在嵌套时index没办法重复用,内循环与外循环不能共用一个index <swiper-item v-for="(items,index) in swiperList" ...
 - http连接基础类,负责底层的http通信
			
/// <summary> /// http连接基础类,负责底层的http通信 /// </summary> public class HttpService { public ...
 - 自定义MVC实现登录案例
			
MVC框架: MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离 ...
 - js 类数组对象arguments
			
function Add() { for (var i = 0; i < arguments.length; i++) { console.log(arguments[i]); } } Add( ...
 - __x__(22)0907第四天__ 垂直外边距重叠
			
外边距重叠, 也叫“外边距合并”,指的是,当两个外边距相遇时,它们将形成一个外边距. 合并后的外边距的高度,等于两个发生合并的外边距的高度中的较大者...在布局时,易造成混淆. 1. 上下元素 垂直外 ...
 - 16进制转化8进制---map
			
#include "stdio.h" #include "string.h" #include "string" #include &quo ...
 - Redis 中可以存储的五种基本类型
			
具体介绍 数字还是字符? String(字符串) 二进制安全 可以包含任何数据,比如jpg图片或者序列化的对象,一个键最大能存储512M --- Hash(字典) 键值对集合,即编程语言中的Map类型 ...
 - Havel-Hakimi定理的方法来构图
			
给定一组非负数字,(数字为节点的度),判断该组数字能不能构成图. Havel-Hakimi定理: 将序列按照从大到小排序之后,从第二个数开始到第一个数的长度+1为止,依次减1.每操作一次,删掉第一个数 ...
 
			
		
Note: