LeetCode 505. The Maze II
原题链接在这里:https://leetcode.com/problems/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.
题解:
Use PriorityQueue to make sure smaller weight is polling out first.
Mark the current position visited when polling out the node, that means from start to this node, the shortest path has been found.
If polling out node has been marked as visited, then it means other shorter path has visited this node before.
Time Complexity: O(mn*logmn). m = maze.length. n = maze[0].length.
Space: O(mn).
AC Java:
1 class Solution { 2 int [][] dirs = new int[][]{{-1,0}, {1,0}, {0,-1}, {0,1}}; 3 4 public int shortestDistance(int[][] maze, int[] start, int[] destination) { 5 if(maze == null || maze.length == 0 || maze[0].length == 0){ 6 return -1; 7 } 8 9 int m = maze.length; 10 int n = maze[0].length; 11 boolean [][] visited = new boolean[m][n]; 12 PriorityQueue<int []> minHeap = new PriorityQueue<>((a,b) -> a[2]-b[2]); 13 minHeap.add(new int[]{start[0], start[1], 0}); 14 while(!minHeap.isEmpty()){ 15 int [] cur = minHeap.poll(); 16 17 // If smaller value has been found for cur before, skip 18 if(visited[cur[0]][cur[1]]){ 19 continue; 20 } 21 22 visited[cur[0]][cur[1]] = true; 23 if(cur[0] == destination[0] && cur[1] == destination[1]){ 24 return cur[2]; 25 } 26 27 for(int [] dir : dirs){ 28 int r = cur[0]; 29 int c = cur[1]; 30 int step = 0; 31 while(r+dir[0]>=0 && r+dir[0]<m && c+dir[1]>=0 && c+dir[1]<n && maze[r+dir[0]][c+dir[1]]==0){ 32 r += dir[0]; 33 c += dir[1]; 34 step++; 35 } 36 37 minHeap.add(new int[]{r, c, cur[2]+step}); 38 } 39 } 40 41 return -1; 42 } 43 }
类似The Maze.
原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/11926729.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- CodeForces - 262C 贪心
- 花狗C语言彩色贪吃蛇(完整代码)
- CodeForces - 262B
- CodeForces - 260B
- 蓝桥杯第九届C语言C组第一题:哪天返回
- java学习之路:17.掌握Boolean对象的创建以及Boolean类提供的各种方法
- java学习之路:16.掌握Integer,Long,Short对象的创建以及其类提供的各种方法
- java学习之路:15.对象的创建,属性,行为,引用,比较,销毁
- java学习之路:14.类的构造方法,静态变量,常量和方法,类的主方法
- java学习之路:13.类(成员变量,成员方法,权限修饰符,局部变量及有效范围,this关键字)
- 线性表--定长顺序串(十四)
- java学习之路:10.数组的基本操作(遍历,替换,排序,复制,查询)
- java学习之路:9.一,二维数组创建初始化
- 线性表--堆串(十五)
- 图解PostgreSQL-buffer管理(二)