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

一刷
题解:
同LC490, 但是这里不仅要碰到壁才能停下,还要找出最短的路径。
最值都可以用动态规划(cache的思路,把已经存在的最小值保存下来),也就是构造length[m][n]的array, 保存经过点(i, j)的当前最小值,如果新的路径经过改点,比最小值大,直接跳过,否则update最小值。由于是求最值,并且要撞墙才会停,于是要遍历到图中每个点。

class Solution {
    class Point {
        int x,y,l;
        public Point(int x, int y, int l) {
            this.x=x;
            this.y=y;
            this.l=l;
        }
    }
    
    
    public int shortestDistance(int[][] maze, int[] start, int[] destination) {
        int m = maze.length, n = maze[0].length;
        int[][] length = new int[m][n];
        for(int[] len : length){
            Arrays.fill(len, Integer.MAX_VALUE);
        }
        int[][] dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
        PriorityQueue<Point> list = new PriorityQueue<>((o1, o2)->o1.l - o2.l);
        list.offer(new Point(start[0], start[1], 0));
        while(!list.isEmpty()){
            Point p = list.poll();
            if(length[p.x][p.y]<=p.l) continue;//already have found a shorter route
            length[p.x][p.y] = p.l;
            for(int[] dir: dirs){
                int xx = p.x;
                int yy = p.y;
                int l = p.l;
                while(xx>=0 && xx<m && yy>=0 && yy<n && maze[xx][yy] == 0){
                    xx+=dir[0];
                    yy+=dir[1];
                    l++;
                }
                xx-=dir[0];
                yy-=dir[1];
                l--;
                list.offer(new Point(xx, yy, l));
            }
        }
        return length[destination[0]][destination[1]]==Integer.MAX_VALUE?-1:length[destination[0]][destination[1]];
    }
}

二刷
同上

class Solution {
    
    class Point implements Comparable<Point>{
        int x,y,l;
        public Point(int _x, int _y, int _l) {x=_x;y=_y;l=_l;}
        
        @Override
        public int compareTo(Point o){
            return this.l - o.l;
        }
    }
    
    public int shortestDistance(int[][] maze, int[] start, int[] destination) {
        int m=maze.length, n=maze[0].length;
        int[][] length = new int[m][n];
        int[][] dirs = {{-1,0},{0,1},{1,0},{0,-1}};
        for(int[] len : length) Arrays.fill(len, Integer.MAX_VALUE);
        PriorityQueue<Point> pq = new PriorityQueue<>();
        pq.offer(new Point(start[0], start[1], 0));
        while(!pq.isEmpty()){
            Point cur = pq.poll();
            for(int[] dir : dirs){
                int x = cur.x;
                int y = cur.y;
                int l = cur.l;
                while(x>=0 && x<m && y>=0 && y<n && maze[x][y] == 0){
                    x += dir[0];
                    y += dir[1];
                    l++;
                }
                l--;
                x -= dir[0];
                y -= dir[1];
                if(length[x][y]<=l) continue;
                length[x][y] = l;
                //System.out.println(x+"/"+y+"/"+l);
                pq.offer(new Point(x, y, l));
            }
        }
        return length[destination[0]][destination[1]]==Integer.MAX_VALUE?-1:length[destination[0]][destination[1]];
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • There is a ball in a maze with empty spaces and walls. Th...
    matrxyz阅读 867评论 0 0
  • The Little Prince Day8 Then he added: “So you, too, come ...
    来日缘何方长阅读 1,555评论 0 0
  • 满纸荒唐言为谁,一把心酸泪付水。 芊芊女儿才情出,唯唯男儿情才拙。 一席情烈真心倾,千翻心思奈何归。 曲终人散佳人...
    灯下漫谈的女孩阅读 177评论 0 0
  • 再看龙与虎,感动依旧,龙虎的爱情令我陶醉,曾经龙虎给我树立了爱情价值观,彼此的喜欢,彼此的信任,彼此的不离不弃,在...
    还未着魔阅读 914评论 0 1
  • 一元关系:以自我为中心;二元关系:心里能够住着另一个自己爱的且他也爱自己的人;三元关系:内心能够能够住下更多的人,...
    w小郭阅读 828评论 0 0