每日一题:地图分析(多源最短路)/(动态规划)

你现在手里有一份大小为 N x N 的『地图』(网格) grid,上面的每个『区域』(单元格)都用 0 和 1 标记好了。其中 0 代表海洋,1 代表陆地,你知道距离陆地区域最远的海洋区域是是哪一个吗?请返回该海洋区域到离它最近的陆地区域的距离。

我们这里说的距离是『曼哈顿距离』( Manhattan Distance):(x0, y0) 和 (x1, y1) 这两个区域之间的距离是 |x0 - x1| + |y0 - y1| 。

如果我们的地图上只有陆地或者海洋,请返回 -1。

示例1:
输入:[[1,0,1],[0,0,0],[1,0,1]]
输出:2
解释:
海洋区域 (1, 1) 和所有陆地区域之间的距离都达到最大,最大距离为 2。

示例2:
输入:[[1,0,0],[0,0,0],[0,0,0]]
输出:4
解释:
海洋区域 (2, 2) 和所有陆地区域之间的距离都达到最大,最大距离为 4。

提示:

  1. 1 <= grid.length == grid[0].length <= 100
  2. grid[i][j] 不是 0 就是 1

以前做过类似的题——橘子腐烂的问题。

方法一:多源广度优先搜索

BFS的过程就是求最短路的过程,对于多个陆地结点,可以从陆地结点开始进行多源广度优先搜素,从陆地向外,每搜一圈距离加一。所谓多源其实和单源没啥区别,可以看成是单源的第二步就扩出了这么多的节点。

class Solution {
public:
    int maxDistance(vector<vector<int>>& grid) {
        queue<pair<int, int> > bfs;
        int row = grid.size(), column = grid[0].size();
        int maxDistance = INT_MIN;
        int addX[4] = {0, 0, 1, -1};
        int addY[4] = {1, -1, 0, 0};
        for (int i = 0; i < row; i++){
            for (int j = 0; j < column; j++){
                if (grid[i][j] == 1){
                    bfs.push(make_pair(i, j));
                }
            }
        }
        if (bfs.size() == 0 || bfs.size() == row * column) return -1;

        while (!bfs.empty()){
            pair<int, int> temp = bfs.front();
            bfs.pop();
            for (int i = 0; i < 4; i++){
                int newX = temp.first + addX[i], newY = temp.second + addY[i];
                if (newX >= 0 && newX < row && newY >= 0 && newY < column){
                    if (grid[newX][newY] == 0){
                        grid[newX][newY] = grid[temp.first][temp.second] + 1;
                        maxDistance = max(maxDistance, grid[newX][newY]);
                        bfs.push(make_pair(newX, newY));
                    }
                }
            }
        }
        return maxDistance - 1;
    }
};

方法二:多源最短路

可以将多个原点看做一个原点超集,然后只需求这个原点超集到目标点集的最短路径即可。可以使用修改版的Dijkstra算法或者SPFA算法。其实有点像方法一。

/*
多源最短路Dijkstra算法
*/
class Solution{
public:
    static constexpr int MAX_N = 100 + 5;
    static constexpr int INF = int(1E6);
    static constexpr int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};

    int n;
    int d[MAX_N][MAX_N];

    struct Status {
        int v, x, y;
        bool operator < (const Status &rhs) const {
            return v > rhs.v;
        }
    };

    priority_queue <Status> q;

    int maxDistance(vector<vector<int> > &grid){
        this->n = grid.size();
        auto &a = grid;

        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                d[i][j] = INF;
            }
        }

        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (a[i][j]) {
                    d[i][j] = 0;
                    q.push({0, i, j});
                }
            }
        }

        while (!q.empty()) {
            auto f = q.top();
            q.pop();
            for (int i = 0; i < 4; i++){
                int nx = f.x + dx[i], ny = f.y + dy[i];
                if (!(nx >= 0 && nx <= n - 1 && ny >= 0 && ny <= n - 1)) continue;
                if (f.v + 1 < d[nx][ny]){
                    d[nx][ny] = f.v + 1;
                    q.push({d[nx][ny], nx, ny});
                }
            }
        }
        int ans = -1;
        for (int i = 0; i < n; i++){
            for (int j = 0; j < n; j++){
                if (!a[i][j]) ans = max(ans, d[i][j]);
            }
        }

        return (ans == INF) ? -1 : ans;
    }
};

方法三:动态规划

考虑把陆地区域作为源点集、海洋区域作为目标点集,求最短路的过程。对于每个海洋区域(x,y),离它最近的陆地区域到它的路径要么从上方或者左方,要么从右方或者下方。考虑做两次动态规划,第一次从左上到右下,第二次从右下到左上,记f(x,y)为(x,y)距离最近的陆地区域的曼哈顿距离,则可以推出这样的状态转移方程:
第一阶段:从左上到右下
f ( x , y ) = { 0 , (x,y) is land m i n { f ( x 1 , y ) , f ( x , y 1 ) } + 1 , (x,y) is ocean f(x,y)= \begin{cases} 0, & \text{(x,y) is land}\\ min\{f(x-1, y), f(x, y - 1)\} + 1, & \text{(x,y) is ocean} \end{cases}
第二阶段:从右下到左上
f ( x , y ) = { 0 , (x,y) is land m i n { f ( x + 1 , y ) , f ( x , y + 1 ) } + 1 , (x,y) is ocean f(x,y)= \begin{cases} 0, & \text{(x,y) is land}\\ min\{f(x+1, y), f(x, y+1)\} + 1, & \text{(x,y) is ocean} \end{cases}
初始化的时候把陆地的f值全部预置为0,海洋的f值全部预置为INF,做完两个阶段的动态规划后,我们在所有的不为零的f[i][j]中选出一个最大值即可,如果最终比较出的最大值为INF,就返回-1。

class Solution {
public:
    static constexpr int MAX_N = 100 + 5;
    static constexpr int INF = int(1E6);
    
    int f[MAX_N][MAX_N];
    int n;

    int maxDistance(vector<vector<int>>& grid) {
        this->n = grid.size();
        vector<vector<int>>& a = grid;

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                f[i][j] = (a[i][j] ? 0 : INF);
            }
        }

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (a[i][j]) continue;
                if (i - 1 >= 0) f[i][j] = min(f[i][j], f[i - 1][j] + 1);
                if (j - 1 >= 0) f[i][j] = min(f[i][j], f[i][j - 1] + 1);
            }
        }

        for (int i = n - 1; i >= 0; --i) {
            for (int j = n - 1; j >= 0; --j) {
                if (a[i][j]) continue;
                if (i + 1 < n) f[i][j] = min(f[i][j], f[i + 1][j] + 1);
                if (j + 1 < n) f[i][j] = min(f[i][j], f[i][j + 1] + 1);
            }
        }

        int ans = -1;
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (!a[i][j]) {
                    ans = max(ans, f[i][j]);
                }
            }
        }

        if (ans == INF) return -1;
        else return ans;
    }
};
发布了76 篇原创文章 · 获赞 10 · 访问量 8244

猜你喜欢

转载自blog.csdn.net/weixin_38742280/article/details/105187069