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)的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. [LeetCode] The Maze 迷宫

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

  5. 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 ...

  6. 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 ...

  7. LeetCode 499. The Maze III

    原题链接在这里:https://leetcode.com/problems/the-maze-iii/ 题目: There is a ball in a maze with empty spaces ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. 前端js总结

    1 . 在controller层中的@ResponseBody注解中返回的要是一个对象而不能用字符串. 2 . 给html页面的按钮添加单击事件 $(#login).click( function l ...

  2. Linux下IPC中的信号量PV操作

    代码如下所示,两边对照查看程序!(左图为先运行进程 右图为后运行进程)    运行的效果就是:当左边的进程检测到EOF,释放资源V操作之后,右边的进程会迅速的执行对应的printf的操作! 所有代码文 ...

  3. GA:GA优化BP神经网络的初始权值、阈值,从而增强BP神经网络的鲁棒性—Jason niu

    global p global t global R % 输入神经元个数,此处是6个 global S1 % 隐层神经元个数,此处是10个 global S2 % 输出神经元个数,此处是4个 glob ...

  4. Python学习之MySQLdb模块

    摘要: MySQLdb模块用于操作mysql数据库.1.安装MySQLdb模块 yum install MySQL-python -y2.操作流程①.导入模块: import MySQLdb②.连接数 ...

  5. 在iphone的safari浏览器中,拨打电话,出现系统异常弹框

    这是系统级别的问题,暂时无法解决. IPHONE的safari浏览器电话拨打,前两次点击拨打按钮,会正常弹出系统弹框包含(电话号码,取消,呼叫). 第3次往后,点击按钮会出现另一种系统弹框包含(已阻止 ...

  6. Centos6.5部署vsftpd+mysql认证

    1.FTP传输原理 FTP,文件传输协议,是工作在应用层,基于TCP实现,依赖于互联网即可通讯. 1)连接模式 控制(命令)连接,用来通信,一直在线,客户端随机端口连接服务端TCP:21端口. 数据连 ...

  7. 【C语言程序】基因编码

    输入一个长为n=2k(k≤8)01串s,按照"ABC编码规则"进行编码,ABC编码规则是: A                      //若s串全是0 T(s)=        ...

  8. (72)Wangdao.com第十二天_JavaScript 错误处理机制

    1. Error 实例对象 JavaScript 解析或运行时,一旦发生错误,引擎就会抛出一个错误对象. JavaScript 原生提供Error构造函数,所有抛出的错误都是这个构造函数的实例. va ...

  9. 10.1jihe

    两种操作,1是加入数字,二是找最接近的 用set或者平衡二叉树就好了 只写了二叉树的,套版子就好 #include<bits/stdc++.h> #define sf scanf #def ...

  10. [LeetCode] Chalkboard XOR Game 黑板亦或游戏

    We are given non-negative integers nums[i] which are written on a chalkboard.  Alice and Bob take tu ...