LeetCode #317 - Shortest Distance from All Buildings

本文探讨了在一个包含建筑物和障碍物的二维网格中,如何找到一个空地来建造房屋,使得该房屋到所有建筑物的总距离最短。通过使用广度优先搜索(BFS)策略,我们能够遍历所有可能的空地并计算其到各建筑物的最短距离,最终确定最佳建房位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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.

Example:

Input: [[1,0,2,0,1],[0,0,0,0,0],[0,0,1,0,0]]

1 - 0 - 2 - 0 - 1

|     |     |     |    |

0 - 0 - 0 - 0 - 0

|     |     |    |     |

0 - 0 - 1 - 0 - 0

Output: 7 

Explanation: Given three buildings at (0,0), (0,4), (2,2), and an obstacle at (0,2), 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.

class Solution {
public:
    int shortestDistance(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        vector<vector<int>> dist(m, vector<int>(n, 0)); // 表示每个空节点到所有门的最短距离之和
        vector<vector<int>> moves={{-1,0}, {1,0}, {0,-1}, {0,1}};
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == 1) // 发现一扇门,就从这扇门出发,根据BFS遍历所有的空节点,保证距离最小
                {
                    vector<vector<int>> temp=grid; // temp复制grid,0表示没有遍历过的空节点
                    queue<vector<int>> q;
                    q.push({i, j, 0});
                    while(q.size() > 0)
                    {
                        vector<int> p=q.front(); // 队列头元素,p[0]是横坐标,p[1]是纵坐标,p[2]表示从门到该节点的距离
                        q.pop();
                        for(auto move: moves)
                        {
                            int  x=p[0]+move[0];
                            int y=p[1]+move[1];
                            if(x>=0&&x<m&&y>= 0&&y< n&&temp[x][y]==0)
                            {
                                temp[x][y]=p[2]+1;
                                dist[x][y]+=temp[x][y];
                                q.push({x, y, temp[x][y]});
                            }
                        }
                    }
	    // 对于每一扇门,如果这扇门遍历不到一个空节点,就要把它置为-1,表示它是一个达到不到的节点
                    for(int i = 0; i < m; i++)
                        for(int j = 0; j < n; j++)
                            if(temp[i][j] == 0) grid[i][j] = -1;
                }
            }
        }
        
        int result = INT_MAX;
        for(int i = 0; i < m; i++)
        {
            for(int j = 0; j < n; j++)
            {
                if(grid[i][j] == 0)
                    result = min(result, dist[i][j]);
            }
        }
        if(result == INT_MAX) return -1;
        else return result;
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值