Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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 @R_982_10586@l 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,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,1,0]]
2 var sol = Solution()
3 print(sol.shortestDistance(&arr))
4 //Print 7

大佬总结

以上是大佬教程为你收集整理的[Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings全部内容,希望文章能够帮你解决[Swift]LeetCode317. 建筑物的最短距离 $ Shortest Distance from All Buildings所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。