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

     1 class Solution {
     2     public int shortestDistance(int[][] maze, int[] start, int[] destination) {
     3         int row = maze.length;
     4         int col = maze[0].length;
     5         int res = 0;
     6         Queue<Cell> queue = new LinkedList<>();
     7         int[][] dists = new int[row][col];
     8         for (int[] dist: dists) {
     9             Arrays.fill(dist, -1);
    10         }
    11         queue.offer(new Cell(start[0], start[1]));
    12         dists[start[0]][start[1]] = 0;
    13         int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    14         while (!queue.isEmpty()) {
    15             int size = queue.size();          
    16                 Cell cur = queue.poll();
    17                 int curX = cur.x;
    18                 int curY = cur.y;
    19 
    20                 for (int[] direction: directions) {
    21                     int newX = curX;
    22                     int newY = curY;                
    23                     int curDist = dists[curX][curY];
    24 
    25                     while (newX >= 0 && newX < row && newY >= 0 && newY < col && maze[newX][newY] == 0) {
    26                         newX += direction[0];
    27                         newY += direction[1];
    28                         curDist += 1;
    29                     }
    30                     newX -= direction[0];
    31                     newY -= direction[1];
    32                     curDist -= 1;
    33                     if (dists[newX][newY] == -1 || curDist < dists[newX][newY]) {
    34                         dists[newX][newY] = curDist;
    35                         queue.offer(new Cell(newX, newY));
    36                     }
    37                 }
    38 
    39         }
    40         return dists[destination[0]][destination[1]];
    41     }
    42 }
    43 
    44 
    45 class Cell {
    46     int x;
    47     int y;
    48     public Cell(int x, int y) {
    49         this.x = x;
    50         this.y = y;
    51     }
    52 }
  • 相关阅读:
    测试软件—禅道BUG管理工具
    C语言 线性表的操作~(未完)
    数据库考纲~
    圣杯布局和双飞翼布局总局
    总结布局用法
    springboot~入门第三篇~与mybatis整合~(未完)
    微信小程序里 wx:for和wx:for-item区别(补充下wx:key)
    对比下小程序语法和Vue语法异同
    视频转换 rtsp 流 转rtmp流播放(待完善)
    Vue钩子函数~
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12004513.html
Copyright © 2011-2022 走看看