286. Walls and Gates

286. Walls and Gates


You are given a m x n 2D grid initialized with these three possible values.

-1 - A wall or an obstacle.
0 - A gate.
INF - Infinity means an empty room. We use the value 231 - 1 = 2147483647 to represent INF as you may assume that the distance to a gate is less than 2147483647.
Fill each empty room with the distance to its nearest gate. If it is impossible to reach a gate, it should be filled with INF.

Example:

Given the 2D grid:

INF  -1  0  INF
INF INF INF  -1
INF  -1 INF  -1
  0  -1 INF INF

After running your function, the 2D grid should be:

  3  -1   0   1
  2   2   1  -1
  1  -1   2  -1
  0  -1   3   4

http://www.cnblogs.com/grandyang/p/5285868.html

方法1: BFS

BFS的解法: 需要借助queue,我们首先把门的位置都排入queue中, 数据结构用pair<int, int> 来记录门的位置。开始循环,对于门位置的四个相邻点,我们判断其是否在矩阵范围内,并且位置值是否大于上一位置的值加1,如果满足这些条件,我们将当前位置赋为上一位置加1,并将次位置排入queue中,这样等queue中的元素遍历完了,所有位置的值就被正确地更新了。

// BFS
class Solution1 {
public:
    void wallsAndGates(vector<vector<int>>& rooms) {
        if (rooms.empty() || rooms[0].empty()) return;
        queue<pair<int, int>> myQueue;
        vector<vector<int>> directions{{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
        
        // 第一遍搜索门的位置
        for (int i = 0; i < rooms.size(); i++){
            for (int j = 0; j < rooms[0].size(); j++){
                if (rooms[i][j] == 0) myQueue.push(make_pair(i , j));
            }
        }
        // 从queue里依次取格点,判断是否出界,计算距离当前门的距离。
        // 这里每个节点只被取一次,也就是最先搜索到当前节点的门抢到计算距离的权利。计算之后该节点出队列,不会重复计算,所以仅需要判断是否==INT_MAX
        while (!myQueue.empty()){
            //
            int row = myQueue.front().first;
            int col = myQueue.front().second;
            myQueue.pop();
            
            if (row > 0 && rooms[row - 1][col] == INT_MAX){
                rooms[row - 1][col] = rooms[row][col] + 1;
                myQueue.push({row - 1, col});
            }
            
            if (row < rooms.size() -1 && rooms[row + 1][col] == INT_MAX){
                rooms[row + 1][col] = rooms[row][col] + 1;
                myQueue.push({row + 1, col});
            }
            
            if (col > 0 && rooms[row][col - 1] == INT_MAX){
                rooms[row][col - 1] = rooms[row][col] + 1;
                myQueue.push({row, col - 1});
            }
            
            if (col < rooms[0].size() - 1 && rooms[row][col + 1] == INT_MAX){
                rooms[row][col + 1] = rooms[row][col] + 1;
                myQueue.push({row, col + 1});
            }
        }
        return ;
    }
};

方法2: DFS

DFS思路是,我们搜索0的位置,每找到一个0,以其周围四个相邻点为起点,开始DFS遍历,并带入深度值1,如果遇到的值大于当前深度值,我们将位置值赋为当前深度值,并对当前点的四个相邻点开始DFS遍历,注意此时深度值需要加1,这样遍历完成后,所有的位置就被正确地更新了

代码简洁速度快

// DFS
// 
class Solution {
public:
    void wallsAndGates(vector<vector<int>>& rooms) {
        for (int i = 0; i < rooms.size(); ++i) {
            for (int j = 0; j < rooms[i].size(); ++j) {
                if (rooms[i][j] == 0) dfs(rooms, i, j, 0);
            }
        }
    }
    void dfs(vector<vector<int>>& rooms, int i, int j, int val) {
        if (i < 0 || i >= rooms.size() || j < 0 || j >= rooms[i].size() || rooms[i][j] < val) return;
        rooms[i][j] = val;
        dfs(rooms, i + 1, j, val + 1);
        dfs(rooms, i - 1, j, val + 1);
        dfs(rooms, i, j + 1, val + 1);
        dfs(rooms, i, j - 1, val + 1);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值