[Locked] Walls and Gates

Walls and Gates

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

  1. -1 - A wall or an obstacle.
  2. 0 - A gate.
  3. 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.

For 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

分析:

  深搜DFS即可,注意剪枝,注意overflow

代码:

//深搜,三种情况return: 房间编号已经比当前距离小的,墙和门,访问过的,而这三种情况可以用一个式子表示grids[i][j] < dist
void dfs(vector<vector<int> > &grids, int dist, int i, int j) {
    if(grids[i][j] < dist)
        return;
    grids[i][j] = dist;
    dfs(grids, dist + 1, i, j + 1);
    dfs(grids, dist + 1, i + 1, j);
    dfs(grids, dist + 1, i, j - 1);
    dfs(grids, dist + 1, i - 1, j);
    return;
}
void distanceFromGate(vector<vector<int> > &grids) {
    if(grids.empty())
        return;
    //设立边界岗哨
    grids.insert(grids.begin(), vector<int> (grids[0].size(), -1));
    grids.push_back(vector<int> (grids[0].size(), -1));
    for(auto &row : grids) {
        row.insert(row.begin(), -1);
        row.push_back(-1);
    }
    //从每个0开始进行递归
    for(int i = 0; i < grids.size(); i++)
        for(int j = 0; j < grids[0].size(); j++)
            if(grids[i][j] == 0)
               dfs(grids, 0, i, j);
    //除去边界岗哨
    grids.erase(grids.begin());
    grids.pop_back();
    for(auto &row : grids) {
        row.erase(row.begin());
        row.pop_back();
    }
    return;
}
时间: 2024-08-11 01:36:10

[Locked] 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. - 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

LeetCode Walls and Gates

原题链接在这里:https://leetcode.com/problems/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 t

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 ga

Walls and Gates -- LeetCode

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 ga

LeetCode 286: Walls and Gates

Note: Need to skipp the 0 element update room but cannot skip BFS from that point class Solution { public void wallsAndGates(int[][] rooms) { if (rooms.length == 0 || rooms[0].length == 0) { return; } for (int i = 0; i < rooms.length; i++) { for (int

[LeetCode] 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 ga

leetcode 锁掉的题目清单

也刷leetcode, 先把锁掉的题目留备份好了: 156 Binary Tree Upside Down  [1] Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tre

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.