志,敢教日月换新天。为有牺牲多壮

[Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: https://www.cnblogs.com/strengthen/p/10706631.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

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 2, where:

  • Each 0 marks an empty land which you can pass by freely.
  • Each 1 marks a building which you cannot pass through.
  • Each 2 marks an obstacle which you cannot pass through.

For example, given three buildings at (0,0)(0,4)(2,2), and an obstacle at (0,2):

1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0

The point (1,2) is an ideal empty land to build a house, as the total travel distance of 3+3+1=7 is minimal. So return 7.

Note:
There will be at least one building. If it is not possible to build such house according to the above rules, return -1.


你想在一片空旷的土地上建造一座房子,它能在最短的距离内到达所有的建筑物。你只能上下左右移动。您将得到一个值为0、1或2的二维网格,其中:

每0分代表一片空地,你可以自由通行。

每1个标记一个不能穿过的建筑物。

每2个标记一个你不能通过的障碍物。

例如,假设(0,0)(0,4)(2,2)处有三座建筑物,而(0,2)处有障碍物:

1 - 0 - 2 - 0 - 1
|   |   |   |   |
0 - 0 - 0 - 0 - 0
|   |   |   |   |
0 - 0 - 1 - 0 - 0

由于3+3+1=7的总行程最小,因此点(1,2)是建造房屋的理想空地。所以返回7。

注:

至少有一栋楼。如果无法按照上述规则建造此类房屋,则返回-1。


Solution:

复制代码
 1 class Solution {
 2     func shortestDistance(_ grid:inout [[Int]]) -> Int {
 3         var res:Int = Int.max
 4         var val:Int = 0
 5         var m:Int = grid.count
 6         var n:Int = grid[0].count
 7         var sum:[[Int]] = grid
 8         var dirs:[[Int]] = [[0,-1],[-1,0],[0,1],[1,0]]
 9         for i in 0..<grid.count
10         {
11             for j in 0..<grid[i].count
12             {
13                 if grid[i][j] == 1
14                 {
15                     res = Int.max
16                     var dist:[[Int]] = grid
17                     var q:[(Int,Int)] = [(Int,Int)]()
18                     q.append((i, j))
19                     while(!q.isEmpty)
20                     {
21                         var a:Int = q.first!.0
22                         var b:Int = q.first!.1
23                         q.removeFirst()
24                         for k in 0..<dirs.count
25                         {
26                             var x:Int = a + dirs[k][0]
27                             var y:Int = b + dirs[k][1]
28                             if x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == val
29                             {
30                                 grid[x][y] -= 1
31                                 dist[x][y] = dist[a][b] + 1
32                                 sum[x][y] += (dist[x][y] - 1)
33                                 q.append((x, y))
34                                 res = min(res, sum[x][y])
35                             }
36                         }
37                     }
38                     val -= 1
39                 }
40             }
41         }
42         return res == Int.max ? -1 : res    
43     }
44 }
复制代码

点击:Playground测试

1 var arr:[[Int]] = [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]
2 var sol = Solution()
3 print(sol.shortestDistance(&arr))
4 //Print 7

 

posted @   为敢技术  阅读(615)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示
哥伦布
09:09发布
哥伦布
09:09发布
3°
多云
东南风
3级
空气质量
相对湿度
47%
今天
中雨
3°/15°
周三
中雨
3°/13°
周四
小雪
-1°/6°