资讯专栏INFORMATION COLUMN

leetcode 317 shortest distance from all buildings

yanbingyun1990 / 3339人阅读

摘要:从第一个点出发表示空地,表示已经走过的空地,避免重复。看起来就像一层层的涂色。

1   0   2   0   1

0   0   0   0   0

0   0   1   0   0

第一个building, 把涂色把0变成-1, 同一层最终会涂成相同颜色-1

  1    -1    2   -1    1  

 -1    -1   -1   -1   -1

 -1    -1    1   -1   -1

距离矩阵

0   1   0   5   0

1   2   3   4   5

2   3   0   5   6

第一个building, 把涂色把-1变成-2, 同一层最终会涂成相同颜色-2

  1    -2    2   -2    1  

 -2    -2   -2   -2   -2

 -2    -2    1   -2   -2

距离矩阵

0   6   0   6   0

6   6   6   6   6

8   8   0   8   8

第一个building, 把涂色把-2变成-3, 同一层最终会涂成相同颜色-3

  1    -3    2   -3    1  

 -3    -3   -3   -3   -3

 -3    -3    1   -3   -3

距离矩阵

0    9    0   9   0

9    8    7   8   9

10   9    0   9   10

为了避路径重复,我们有两种方法,一种是用额外的空间visited, 一种是改变输入。
从第一个点出发0表示空地,-1表示已经走过的空地,避免重复。
从第二个点出发-1表示空地,-2表示已经走过的空地,避免重复。
看起来就像一层层的涂色。

public class Solution {
    private int[] dx = {0, 1, 0, -1},  dy = {1, 0, -1, 0};
    private int min = Integer.MAX_VALUE;
    
    public int shortestDistance(int[][] grid) {
        if(grid == null || grid.length == 0) return 0;
        int m = grid.length, n = grid[0].length;
        int[][] distance = new int[m][n];                   // 记录累加距离
        int start = 0;
        for(int i=0; i q = new ArrayDeque<>();
         q.offer(new int[]{i,j});                           
         int level = 0, m = grid.length, n = grid[0].length;
         min = Integer.MAX_VALUE;
         
         while(!q.isEmpty()){
             int size = q.size();                       // 涂色的时候,记录当前层的大小
             level++;                                   // 进入下一层,需要增加距离
             for(int k=0; k=0 && y>=0 && x           
               
                                           
                       
                 

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66713.html

相关文章

  • [LeetCode] 317. Shortest Distance from All Buildin

    Problem You want to build a house on an empty land which reaches all buildings in the shortest amount of distance. You can only move up, down, left and right. You are given a 2D grid of values 0, 1 or...

    wall2flower 评论0 收藏0
  • Shortest Distance from All Buildings

    摘要:如果该没有被之前所有的访问过,就不可能成为答案根据要求的位置能到所有的,其他与它相邻的点也是这样。和用矩阵比,缩小了每次遍历的范围。 Shortest Distance from All Buildings 题目链接:https://leetcode.com/problems... 这道题要求最短的距离,一般这种要求可以到的地方的距离,都需要把整个图遍历一遍,遍历一般就是bfs和dfs...

    DC_er 评论0 收藏0
  • [LeetCode] 126. Word Ladder II

    摘要:存放过程中的所有集合为所有的结尾,则顺序存放这个结尾对应的中的所有存放同一个循环的新加入的,在下一个循环再依次对其中元素进行进一步的把首个字符串放入新,再将放入,并将键值对放入,进行初始化 Problem Given two words (start and end), and a dictionary, find all shortest transformation sequenc...

    wayneli 评论0 收藏0
  • [LeetCode] Shortest Distance to a Character

    Problem Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string. Example 1: Input: S = loveleetcode, C = eOutput: [3, 2, 1...

    blankyao 评论0 收藏0
  • [Leetcode] Shortest Word Distance 最短单词间距

    摘要:代码第一次写入就先不比较第一次写入就先不比较哈希表法复杂度时间空间思路因为会多次调用,我们不能每次调用的时候再把这两个单词的下标找出来。我们可以用一个哈希表,在传入字符串数组时,就把每个单词的下标找出存入表中。 Shortest Word Distance Given a list of words and two words word1 and word2, return the ...

    jsliang 评论0 收藏0

发表评论

0条评论

yanbingyun1990

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<