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:

  1. There is only one ball and one destination in the maze.
  2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
  3. 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.
  4. The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.
 public class Solution {
public int shortestDistance(int[][] maze, int[] start, int[] destination) {
if (maze == null || start ==null || destination == null) return ; int[] dx = new int[]{, , , -};
int[] dy = new int[] {, -, ,};
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{start[], start[], });
boolean[][] isVisited = new boolean[maze.length][maze[].length];
int min = Integer.MAX_VALUE; while (!queue.isEmpty()) {
int[] point = queue.poll();
isVisited[point[]][point[]] = true;
if (min!= Integer.MAX_VALUE && point[]>=min) continue; //if step is greater than min, we don't need to continue
if (point[] == destination[] && point[] == destination[]) {
min = Math.min(min, point[]);
}
for (int k=; k<; k++) {
int x = point[];
int y = point[];
int step = point[];
while (x+dx[k]>= && x+dx[k] < maze.length && y+dy[k]>= && y+dy[k]<maze[].length && maze[x+dx[k]][y+dy[k]]==) {
x = x+ dx[k];
y = y+ dy[k];
step++;
}
if (!isVisited[x][y]) {
queue.offer(new int[]{x, y, step});
}
}
} return min == Integer.MAX_VALUE? - : min;
}
}

最新文章

  1. JqueryQrcode生成二维码不支持中文的解决办法
  2. title换行
  3. 为什么我还不推荐内存中OLTP给用户
  4. iredmail安装脚本分析(一)---iRedmail.sh
  5. C#微信公众号开发之网页授权oauth2.0获取用户基本信息(一)
  6. 二、中间件(middleware)
  7. CSS3实现气泡效果
  8. ARM的两种启动方式 (NAND FLASH. NOR FLASH)
  9. 彩蛋 Python之道
  10. git文件夹下项目更改ip地址小结
  11. 2981:大整数加法-poj
  12. Redis学习笔记~Twenproxy所起到的作用
  13. Centos6安装SaltStack
  14. 统计分页一些sql
  15. 排查java进程cpu占用高的问题
  16. layui之select的option叠加问题解决
  17. winform clickonce在线安装
  18. PAT甲题题解-1068. Find More Coins (30)-dp,01背包
  19. git快捷命令缩写
  20. SQL基础培训实战教程[全套]

热门文章

  1. Codeforces 1276C/1277F/1259F Beautiful Rectangle (构造)
  2. springboot实现异步调用
  3. pre-fork 分叉 软分叉 硬分叉 前叉实现 pre-fork implementation
  4. express利用nodemailer发送邮件(163邮箱)
  5. otter安装、使用
  6. 一个有趣的BUG/按钮disabled之后还能触发click事件
  7. Android:JACK编译错误汇总及解决
  8. 各个处理器架构ISA编程指南
  9. shell生成指定范围随即整数
  10. Flask之加载静态资源