Leetcode: The Maze III(Unsolved Lock Problem)
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolling up (u), down (d), left (l) or right (r), but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction. There is also a hole in this maze. The ball will drop into the hole if it rolls on to the hole. Given the ball position, the hole position and the maze, find out how the ball could drop into the hole by moving the shortest distance. The distance is defined by the number of empty spaces traveled by the ball from the start position (excluded) to the hole (included). Output the moving directions by using 'u', 'd', 'l' and 'r'. Since there could be several different shortest ways, you should output the lexicographically smallest way. If the ball cannot reach the hole, output "impossible". 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 ball and the hole coordinates are represented by row and column indexes. Example 1 Input 1: a maze represented by a 2D array 0 0 0 0 0
1 1 0 0 1
0 0 0 0 0
0 1 0 0 1
0 1 0 0 0 Input 2: ball coordinate (rowBall, colBall) = (4, 3)
Input 3: hole coordinate (rowHole, colHole) = (0, 1) Output: "lul"
Explanation: There are two shortest ways for the ball to drop into the hole.
The first way is left -> up -> left, represented by "lul".
The second way is up -> left, represented by 'ul'.
Both ways have shortest distance 6, but the first way is lexicographically smaller because 'l' < 'u'. So the output is "lul".Example 2 Input 1: a maze represented by a 2D array 0 0 0 0 0
1 1 0 0 1
0 0 0 0 0
0 1 0 0 1
0 1 0 0 0 Input 2: ball coordinate (rowBall, colBall) = (4, 3)
Input 3: hole coordinate (rowHole, colHole) = (3, 0)
Output: "impossible"
Explanation: The ball cannot reach the hole.Note:
There is only one ball and one hole in the maze.
Both the ball and hole 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 the width and the height of the maze won't exceed 30.
Each time, first add the direction to the path, and then go with that direction, checking for hole along the way. When hit a wall, try to turn, and go with the new direction. For the starting point, don't "go", jump directly to "turn" part.
public class Solution {
int min; //min distance to hole
String minS; //min distance's path string
int[] hole;
int[][] maze;
int[][] map; //shortest distant traveling from ball to this point
int[][] dirs = {{0,1},{-1,0},{1,0},{0,-1}}; //r, u, d, l
public String findShortestWay(int[][] maze, int[] ball, int[] hole) {
this.min = Integer.MAX_VALUE;
this.minS = null;
this.hole = hole;
this.maze = maze;
this.map = new int[maze.length][maze[0].length];
for(int i = 0; i<map.length; i++) Arrays.fill(map[i], Integer.MAX_VALUE);
move(ball[0], ball[1], 0, "", -1);
return (minS==null) ? "impossible" : minS;
}
private void move(int r, int c, int cnt, String path, int dir){//dir is a index of dirs
if(cnt > min || cnt > map[r][c]) return; //not a shortest route for sure
if(dir!=-1){//if not from start point
//add path
if(dir==0) path+='r';
else if(dir==1) path+='u';
else if(dir==2) path+='d';
else path+='l';
//roll along dir
while(r>=0 && r<maze.length && c>=0 && c<maze[0].length && maze[r][c]==0){
map[r][c] = Math.min(map[r][c], cnt);
if(r==hole[0] && c==hole[1]){//check hole
if(cnt==min && path.compareTo(minS)<0){
minS=path;
}else if(cnt<min){
min = cnt;
minS = path;
}
return;
}
r += dirs[dir][0];
c += dirs[dir][1];
cnt++;
}
r -= dirs[dir][0];//[r,c] is wall, need to walk back 1 step
c -= dirs[dir][1];
cnt--;
}
//hit wall (or start) -> try to turn
for(int i = 0; i<dirs.length; i++){
if(dir == i) continue;//dont keep going
if(dir == (3-i)) continue;//dont go back
int newR = r + dirs[i][0];
int newC = c + dirs[i][1];
if(newR>=0 && newR<maze.length && newC>=0 && newC<maze[0].length && maze[newR][newC]==0){//can go
move(r, c, cnt, path, i);
}
}
}
}
Leetcode: The Maze III(Unsolved Lock Problem)的更多相关文章
- LC 499. The Maze III 【lock,hard】
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 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 迷宫
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- 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(Unsolved locked problem)
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- LeetCode 499. The Maze III
原题链接在这里:https://leetcode.com/problems/the-maze-iii/ 题目: There is a ball in a maze with empty spaces ...
- [LeetCode] 499. The Maze III 迷宫 III
There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...
- Leetcode: Max Consecutive Ones II(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
随机推荐
- Crypto支付宝模块的安装
沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info 1.将Crypto放到site-packages下 2.OSErro ...
- python实现Hbase
1. 下载thrift 作用:翻译python语言为hbase语言的工具 2. 运行时先启动hbase 再启动thrift,最后在pycharm中通过happybase包连接hbase 在hbase目 ...
- css margin使用技巧
margin使用技巧: (1)设置元素水平居中:margin:x auto; (2)margin负值让元素位移及边框合并 水平居中:auto 代码: <!DOCTYPE html> < ...
- react-native android textinput显示不全的问题
出现的问题 如下图 原因 android 输入框默认带有上下内边距 解决 将Textinput元素样式的垂直内边距设置为0 paddingVertical: 0
- [LeetCode] Design Circular Queue 设计环形队列
Design your implementation of the circular queue. The circular queue is a linear data structure in w ...
- vue-router 之 keep-alive
参考 https://www.jianshu.com/p/0b0222954483
- 安装linux
ctrl+alt tab键切换
- Linux中使用Apache发布html网页
在线学习: https://www.shiyanlou.com/courses/1 工具/原料 Linux,httpd,vi 样例html文件一份 方法/步骤 1 编辑httpd配置文件 2 查找 ...
- P2678 跳石头---(二分答案)
题目背景 一年一度的“跳石头”比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起点和终点.在起点和终点之间,有 NNN 块岩石 ...
- 容器—stack
c++ stl栈stack介绍 C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构. c++ stl栈stack的头文件 ...
Example 2
Input 1: a maze represented by a 2D array
0 0 0 0 0
Note: