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 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]];
}
}

最新文章

  1. SQL Server null知多少?
  2. 多项目开发下的dll文件管理
  3. ruby 2.2
  4. svg path中的贝塞尔曲线
  5. Computer Graphics Research Software
  6. Leetcode: Sort Characters By Frequency
  7. ORACLE之UTL_FILE包详解
  8. Ext TabPanel items高度宽度自适应
  9. 二模10day2解题报告
  10. MySQL锁系列1
  11. Bulk Insert命令具体
  12. Android开源项目发现----其他特殊效果篇(持续更新)
  13. CMOS和TTL的區別
  14. 基于Handler架构的录音程序
  15. Linux基础六
  16. java UTC时间格式化
  17. c#十进制转二进制算法 和字符串反转算法
  18. API手册(2017)
  19. BZOJ.3653.谈笑风生(长链剖分/线段树合并/树状数组)
  20. C++11 列表初始化

热门文章

  1. webpack报错需要合适的loader
  2. 【Codeforces】【图论】【数量】【哈密顿路径】Fake bullions (CodeForces - 804F)
  3. Android系统层次解析
  4. [CF566A]Matching Names
  5. [PA2014]Parking
  6. Java虚拟机----垃圾回收与内存分配
  7. [EOJ Monthly 2018.10][C. 痛苦的 01 矩阵]
  8. Eclipse导出自己的项目(仅供自己保留方式war包)
  9. YARN的基础配置
  10. div+css显示两行或三行文字,超出用...表示