The Maze II (Leetcode 505)

这道题与Maze I 大同小异,不同的是需要search出最短距离。而处理方法也是不同于maze I的 visited数组,在这里采用distance数组来track每个边界点到起点的最短距离。同时当该点距离需要更新时,才做进一步的搜索。

BFS:

class Solution {
public:

    bool isValid(vector<vector<int>>& maze, int x, int y){
        if(x >= 0 && x < maze.size() && y >= 0 && y < maze[0].size() && maze[x][y] == 0) return true;
        return false;
    }
    
    int compute(int x1, int y1, int x2, int y2){
         return abs(x1-x2) + abs(y1-y2);
    }

    int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(maze.empty() || maze[0].empty() || start.empty() || destination.empty()) return -1;
        int row = maze.size(), col = maze[0].size();
        vector<vector<int>> distance(row, vector<int>(col, INT_MAX));
        distance[start[0]][start[1]] = 0;
        queue<pair<int, int>> q;
        q.push({start[0], start[1]});
        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        while(!q.empty()){
            int i = q.front().first, j = q.front().second; 
            q.pop();
            for(auto it : directions){
                int x = i, y = j;
                while(isValid(maze, x + it.first, y + it.second)){
                    x += it.first; y += it.second;
                }
                if(distance[x][y] > distance[i][j] + compute(x, y, i, j)){
                    distance[x][y] = distance[i][j] + compute(x, y, i, j);
                    q.push({x, y});
                }
            }
        }
        return distance[destination[0]][destination[1]] == INT_MAX ? -1 : distance[destination[0]][destination[1]];
    }
};

DFS:

class Solution {
public:

    bool isValid(vector<vector<int>>& maze, int x, int y){
        if(x >= 0 && x < maze.size() && y >= 0 && y < maze[0].size() && maze[x][y] == 0) return true;
        return false;
    }
    
    int compute(int x1, int y1, int x2, int y2){
         return abs(x1-x2) + abs(y1-y2);
    }
    
    void dfs(vector<vector<int>>& maze, vector<vector<int>> &distance, int i, int j){
        int row = maze.size(), col = maze[0].size();
        vector<pair<int, int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
        for(auto it : directions){
            int x = i, y = j;
            while(isValid(maze, x + it.first, y + it.second)){
                x += it.first; y += it.second;
            }
            if(distance[x][y] > distance[i][j] + compute(x, y, i, j)){
                distance[x][y] = distance[i][j] + compute(x, y, i, j);
                dfs(maze, distance, x, y);
            }
        }
    }
    
    int shortestDistance(vector<vector<int>>& maze, vector<int>& start, vector<int>& destination) {
        if(maze.empty() || maze[0].empty() || start.empty() || destination.empty()) return -1;
        int row = maze.size(), col = maze[0].size();
        vector<vector<int>> distance(row, vector<int>(col, INT_MAX));
        distance[start[0]][start[1]] = 0;
        dfs(maze, distance, start[0], start[1]);
        return distance[destination[0]][destination[1]] == INT_MAX ? -1 : distance[destination[0]][destination[1]];
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,776评论 0 33
  • -DFS(Depth First Search):深度优先搜索 访问完一个顶点的所有邻接点之后,会按原路返回,对应...
    Spicy_Crayfish阅读 2,860评论 1 0
  • 贪心算法 贪心算法总是作出在当前看来最好的选择。也就是说贪心算法并不从整体最优考虑,它所作出的选择只是在某种意义上...
    fredal阅读 9,284评论 3 52
  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 17,941评论 2 36
  • 【赏析】24-BREAKING OUT 本章讲述了陈出逃美国和Michael在现实和梦想间挣扎的延续。全书中出现的...
    慢慢树阅读 249评论 0 0