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. HTML 中的预留字符(如标签的小于号 < )必须被替换为字符实体( &lt; )。 不间断空格(&nbsp;)

    1. 参考 HTML 字符实体 Python处理HTML转义字符 比方说一个从网页中抓到的字符串 html = '<abc>' 用Python可以这样处理: import HTMLPars ...

  2. call、apply、bind

    ***call,apply,bind 替换this 何时: 只要this不是想要的都可用call,apply,bind替换 选择: call/apply: *调用*函数,在调用时,*临时*替换函数中的 ...

  3. cache 缓存的处理

    , ) { ? ? ) && ? lstorage.remove(key) : sstorage.remove(key) } /** * 更新缓存配置文件 */ Cache.proto ...

  4. 我的 Putty 配色方案

    首先,右键单击 Putty 顶部边框,在弹出菜单中选择 Change settings,进入颜色设置 Category->Window->Colours 然后,按以下参数配置进行修改: D ...

  5. NEL程序员专用轻钱包 进入0.01状态了

    这个轻钱包能干什么,现在就能在测试网看个余额,转个帐,调用个合约. 而且功能非常程序员化 你会说是不是没啥用   但是他有非常有用,因为他可以很容易的拼出NEOGUI拼不出来的交易 比如参与ICO交易 ...

  6. (52)Wangdao.com第七天_字面量/变量_标识符_数据类型_数据的存储

    JavaScript 字面量 和 变量 字面量:就是那些不可变的值,如1,2,100,2000,Infinity,NaN 变量: 变量,代表的当前随机分配的内存地址. 变量的值,是可变的,可以用来保存 ...

  7. Git 简单入门(一)

    Git 简介 Git 是目前世界上最先进的分布式版本控制系统 分布式和集中式 集中式版本控制系统 版本库放在中央服务器,干活之前先从中央服务器取得最新版本,然后开始干活,活干完后将自己干的成果推送给中 ...

  8. 书上关于*(p++)表达式的几种变形形式的思考题

    代码: int main(){ int a[10] = { 1,2,3,4,5,6,7,8,9,10 }; int *p = &a[3]; cout << "*p++ = ...

  9. 前台js根据当前时间生成订单号

    *********前台显示框**************** <input type="text" id="WIDout_trade_no" name=& ...

  10. Appium-Python-Windows环境搭建笔记

    Appium版本:1.11.0 操作系统:Windows7-64位 开发语言:Python 3.7.2 测试应用平台:安卓 5.1.1 Appium服务端 一.JDK 也许你会觉得很奇怪,我搭建Pyt ...