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, 但是這里不僅要碰到壁才能停下,還要找出最短的路徑。
最值都可以用動(dòng)態(tài)規(guī)劃(cache的思路,把已經(jīng)存在的最小值保存下來(lái)),也就是構(gòu)造length[m][n]的array, 保存經(jīng)過(guò)點(diǎn)(i, j)的當(dāng)前最小值,如果新的路徑經(jīng)過(guò)改點(diǎn),比最小值大,直接跳過(guò),否則update最小值。由于是求最值,并且要撞墻才會(huì)停,于是要遍歷到圖中每個(gè)點(diǎn)。

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]];
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • There is a ball in a maze with empty spaces and walls. Th...
    matrxyz閱讀 877評(píng)論 0 0
  • The Little Prince Day8 Then he added: “So you, too, come ...
    來(lái)日緣何方長(zhǎng)閱讀 1,627評(píng)論 0 0
  • 滿紙荒唐言為誰(shuí),一把心酸淚付水。 芊芊女兒才情出,唯唯男兒情才拙。 一席情烈真心傾,千翻心思奈何歸。 曲終人散佳人...
    燈下漫談的女孩閱讀 205評(píng)論 0 0
  • 再看龍與虎,感動(dòng)依舊,龍虎的愛(ài)情令我陶醉,曾經(jīng)龍虎給我樹(shù)立了愛(ài)情價(jià)值觀,彼此的喜歡,彼此的信任,彼此的不離不棄,在...
    還未著魔閱讀 967評(píng)論 0 1
  • 一元關(guān)系:以自我為中心;二元關(guān)系:心里能夠住著另一個(gè)自己愛(ài)的且他也愛(ài)自己的人;三元關(guān)系:內(nèi)心能夠能夠住下更多的人,...
    w小郭閱讀 892評(píng)論 0 0

友情鏈接更多精彩內(nèi)容