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.
1 public class Solution { 2 int min; //min distance to hole 3 String minS; //min distance's path string 4 int[] hole; 5 int[][] maze; 6 int[][] map; //shortest distant traveling from ball to this point 7 int[][] dirs = {{0,1},{-1,0},{1,0},{0,-1}}; //r, u, d, l 8 public String findShortestWay(int[][] maze, int[] ball, int[] hole) { 9 this.min = Integer.MAX_VALUE; 10 this.minS = null; 11 this.hole = hole; 12 this.maze = maze; 13 this.map = new int[maze.length][maze[0].length]; 14 for(int i = 0; i<map.length; i++) Arrays.fill(map[i], Integer.MAX_VALUE); 15 16 move(ball[0], ball[1], 0, "", -1); 17 return (minS==null) ? "impossible" : minS; 18 } 19 20 private void move(int r, int c, int cnt, String path, int dir){//dir is a index of dirs 21 if(cnt > min || cnt > map[r][c]) return; //not a shortest route for sure 22 if(dir!=-1){//if not from start point 23 //add path 24 if(dir==0) path+='r'; 25 else if(dir==1) path+='u'; 26 else if(dir==2) path+='d'; 27 else path+='l'; 28 29 //roll along dir 30 while(r>=0 && r<maze.length && c>=0 && c<maze[0].length && maze[r][c]==0){ 31 map[r][c] = Math.min(map[r][c], cnt); 32 if(r==hole[0] && c==hole[1]){//check hole 33 if(cnt==min && path.compareTo(minS)<0){ 34 minS=path; 35 }else if(cnt<min){ 36 min = cnt; 37 minS = path; 38 } 39 return; 40 } 41 r += dirs[dir][0]; 42 c += dirs[dir][1]; 43 cnt++; 44 } 45 r -= dirs[dir][0];//[r,c] is wall, need to walk back 1 step 46 c -= dirs[dir][1]; 47 cnt--; 48 } 49 50 //hit wall (or start) -> try to turn 51 for(int i = 0; i<dirs.length; i++){ 52 if(dir == i) continue;//dont keep going 53 if(dir == (3-i)) continue;//dont go back 54 int newR = r + dirs[i][0]; 55 int newC = c + dirs[i][1]; 56 if(newR>=0 && newR<maze.length && newC>=0 && newC<maze[0].length && maze[newR][newC]==0){//can go 57 move(r, c, cnt, path, i); 58 } 59 } 60 } 61 }