Shortest Distance from All Buildings

题目链接:https://leetcode.com/problems...

这道题要求最短的距离,一般这种要求可以到的地方的距离,都需要把整个图遍历一遍,遍历一般就是bfs和dfs。这道题不用dfs的原因是:empty的位置到building的距离是按最小值来算的,用dfs每次如果放入depth不一定是最小的距离,每次都得更新,没有效率。这道题用bfs的原因:一样的原因,因为距离就是按照最小的距离来算的,完全是bfs的思路。
visited一般两种方式:用一个boolean的矩阵,直接改写grid的值。这里用第二种。-grid[i] [j]表示(i, j)点可以reach到的building数目。当grid[i] [j] == # of buildings so far时,证明当前点还没被visited,且当前点被之前所有的buildings都visited过,那么每次bfs只访问这些点。如果该point没有被之前所有的buildings访问过,就不可能成为答案(根据要求empty的位置能到所有的buildings),其他与它相邻的点也是这样。和用boolean矩阵比,缩小了每次遍历的范围。
从每一个building,即grid[i] [j] == 1的点开始做bfs层次遍历。

  1. 用一个distance矩阵来记录(i, j)到所有building的距离和,对每一个building做bfs

  2. 每次bfs的时候,更新distance[i] [j]的值:

    • Queue<int[]> 记录point

    • 更新level += 1

    • go over当前level的全部point

      • if (i, j)在图内&grid[i] [j] = -num:

        • distance[i] [j] += level

        • grid[i] [j] --

        • q.offer(i, j)

  3. go over整个distance数组,当-grid[i] [j] == # of buildings时,更新最小的距离值

public class Solution {public int shortestDistance(int[][] grid) {/* approach: bfs, distance array* for each building, do a bfs, add the distance* variable: num: record number of buildings already searched* visited => use the grid => do -- if visited[i][j] = -num* result: the grid[i][j] == -(number of buildings) is the possible*         find the smallest distance[i][j]*/distance = new int[grid.length][grid[0].length];int num = 0;for(int i = 0; i < grid.length; i++) {for(int j = 0; j < grid[0].length; j++) {if(grid[i][j] == 1) {bfs(grid, i, j, -num);num++;}}}int result = Integer.MAX_VALUE;// find the smallest distancefor(int i = 0; i < grid.length; i++) {for(int j = 0; j < grid[0].length; j++) {if(grid[i][j] == -num) result = Math.min(result, distance[i][j]);}}return result == Integer.MAX_VALUE ? -1 : result;}int[][] distance;int[] dx = new int[] {-1, 1, 0, 0};int[] dy = new int[] {0, 0, -1, 1};private void bfs(int[][] grid, int x, int y, int num) {Queue<int[]> q = new LinkedList();q.offer(new int[] {x, y});int len = 0;while(!q.isEmpty()) {len++;// current levelfor(int j = q.size(); j > 0; j--) {int[] cur = q.poll();// 4 directionsfor(int i = 0; i < 4; i++) {int nx = cur[0] + dx[i], ny = cur[1] + dy[i];if(nx >= 0 && nx < grid.length && ny >= 0 && ny < grid[0].length && grid[nx][ny] == num) {distance[nx][ny] += len;q.offer(new int[] {nx, ny});grid[nx][ny]--;}}}}}
}

Shortest Distance from All Buildings相关推荐

  1. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  2. LeetCode 613. Shortest Distance in a Line --SQL

    LeetCode 613. Shortest Distance in a Line --SQL LeetCode题解专栏:LeetCode题解 我做的所有的LeetCode的题目都放在这个专栏里,大部 ...

  3. Leetcode PHP题解--D49 821. Shortest Distance to a Character

    D49 821. Shortest Distance to a Character 题目链接 821. Shortest Distance to a Character 题目分析 给定一个字符串s和一 ...

  4. PAT甲级1046 Shortest Distance:[C++题解]前缀和

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 用前缀和快速求出一段的和.注意求两段,取最小值. ac代码 #include<bits/stdc++.h> using ...

  5. 【测试点2超时问题】1046 Shortest Distance (20 分)_21行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 The task is really simple: given N exits on a highway which forms ...

  6. 【Leetcode_easy】821. Shortest Distance to a Character

    problem 821. Shortest Distance to a Character 参考 1. Leetcode_easy_821. Shortest Distance to a Charac ...

  7. [PAT A1046]Shortest Distance

    题目 The task is really simple: given N exits on a highway which forms a simple cycle, you are suppose ...

  8. 【PAT甲级 环最短距离】1046 Shortest Distance (20 分) Java、C++

    题目 这题是给你一个环,让你计算两点之间最短距离. 环的任意两点就两条路,只要算出环长和任意一条路的大小,另一条就出来了 时间复杂度O(N) 提前计算前缀长度和即可 题解 C++ 全部测试点通过 #i ...

  9. (C++)1046 Shortest Distance

    #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using ...

最新文章

  1. js 设计模式—工厂模式
  2. Dockerfile 布局的良好实践
  3. 3.依赖注入 spring_di
  4. 跟我一起学.NetCore之WebApi接口裸奔有风险(Jwt)
  5. Effective C++学习第四天
  6. 解放原画师!Wav2Lip 用 AI 听音同步人物口型
  7. JEECGJWT异常捕获强化处理
  8. Kotlin中定义编译时常量
  9. IDC发布人工智能白皮书:信息流引领人工智能新时代
  10. 赢在微创新_小米用互联网颠覆中国市场的内幕与方法
  11. Spring框架实战入门(超全面,超实用)
  12. 林彪-怎样当好一名师长
  13. 实现ucGUI界面中的拼音汉字输入法(T9)
  14. google官方图标 Material icons 全图标一览
  15. R语言 NetCoMi包 Co-occurrence网络图 微生物16S 网络比较 核心物种
  16. 交叉编译ssh服务到ARM开发板
  17. toLua++使用(转)
  18. [后端 springboot] 打包后html无法访问的问题
  19. Python爬虫---爬虫介绍,实战案例
  20. 计算机应用模拟卷,计算机应用期末模拟试卷B卷

热门文章

  1. 指针-数组传参,指针传参
  2. 我爱Java系列---【登录案例】
  3. web安全之windows系统基础
  4. [视频]youku与56客户端DLL却持
  5. 键盘-App监听软键盘按键的三种方式
  6. Unity Game Starter Kit for Windows Store and Windows Phone Store games
  7. 10个帮助你快速调试和排错的小技巧
  8. hdu 1003 Max Sum
  9. 手机QQ浏览器“有码女神”惊现!意在推广二维码
  10. 广西计算机电缆线报价,广西壮族自治区耐火计算机电缆JYPVP32哪里便宜