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