zoukankan      html  css  js  c++  java
  • [LeetCode] 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:

    1. There is only one ball and one destination in the maze.
    2. Both the ball and the destination exist on an empty space, and they will not be at the same position initially.
    3. 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.
    4. The maze contains at least 2 empty spaces, and both the width and height of the maze won't exceed 100.

    迷宫II。题目设定跟490题几乎一样,不过这个题问的是从起点到终点,需要的最短距离是多少。

    思路依然是flood fill类的BFS的做法,但是需要用到一个额外的矩阵记录最短距离。在做BFS的过程中,跟490题类似,还是把能停下的坐标加入queue继续循环,但是在while循环里,坐标每往前走一步,step就要++,以用来记录距离。当球停下的时候,你就有机会结算一下,当前用的步数是否是最短的。

    时间O(mn)

    空间O(mn)

    Java实现

     1 class Solution {
     2     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
     3         int[][] distance = new int[maze.length][maze[0].length];
     4         for (int[] row : distance) {
     5             Arrays.fill(row, Integer.MAX_VALUE);
     6         }
     7         distance[start[0]][start[1]] = 0;
     8         int[][] dirs = {{0, 1}, {0, -1}, { -1, 0}, {1, 0}};
     9         Queue<int[]> queue = new LinkedList<>();
    10         queue.add(start);
    11         while (!queue.isEmpty()) {
    12             int[] current = queue.poll();
    13             for (int[] dir : dirs) {
    14                 int x = current[0] + dir[0];
    15                 int y = current[1] + dir[1];
    16                 int count = 0;
    17                 while (x >= 0 && y >= 0 && x < maze.length && y < maze[0].length && maze[x][y] == 0) {
    18                     x += dir[0];
    19                     y += dir[1];
    20                     count++;
    21                 }
    22                 if (distance[current[0]][current[1]] + count < distance[x - dir[0]][y - dir[1]]) {
    23                     distance[x - dir[0]][y - dir[1]] = distance[current[0]][current[1]] + count;
    24                     queue.add(new int[] {x - dir[0], y - dir[1]});
    25                 }
    26             }
    27         }
    28         return distance[destination[0]][destination[1]] == Integer.MAX_VALUE ? -1 : distance[destination[0]][destination[1]];
    29     }
    30 }

    flood fill题型总结

    LeetCode 题目总结

  • 相关阅读:
    adb获取不了设备List of devices attached
    Appium常用的API函数
    Clevo P950笔记本加装4G模块
    “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址
    使用XTU降低CPU功耗,自动执行不失效
    Clevo P950系列拆机
    Win10的WSL很好用呀
    ubuntu下opencv使用cvNamedWindow()和cvShowImage()出错的解决方法
    2017年研究生数学建模D题(前景目标检测)相关论文与实验结果
    [翻译]怎么阅读一篇论文
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13388851.html
Copyright © 2011-2022 走看看