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

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

 

转载于:https://www.cnblogs.com/strengthen/p/10706631.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐