LeetCode 286. Walls and Gates

本文介绍了一种解决迷宫问题中空房间到最近出口的距离计算算法。通过深度优先搜索(DFS)和广度优先搜索(BFS)两种方法进行比较,详细解释了如何在包含墙壁、空房间及出口的二维网格中找到每个空房间到最近出口的最短距离。

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

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

This method is not good but it is very straightforward.

#include <climits>
#include <vector>
#include <iostream>
using namespace std;

void wallsAndGates(vector< vector<int> >& matrix, int i, int j, vector< vector<bool> >& used, long long int& distance, long long int& minDis) {
  if((i < 0) || (i == matrix.size()) || (j < 0) || (j == matrix[i].size())) {
    return;
  } else {
    if(matrix[i][j] == 0) {
      minDis = min(minDis, distance);
      return;
    }
  }
  vector< vector<int> > direction {
    {0,  1},
    {0, -1},
    {-1, 0},
    {1, 0}};
  for(int k = 0; k < 4; ++k) {
    int next_i = i + direction[k][0];
    int next_j = j + direction[k][1];
    if(next_i >= 0 && next_i < matrix.size() && next_j >= 0 && next_j < matrix[i].size() && (matrix[next_i][next_j] != -1) && (used[next_i][next_j] == false)) {
      distance += 1;
      used[next_i][next_j] = true;
      wallsAndGates(matrix, next_i, next_j, used, distance, minDis);
      distance -= 1;
      used[next_i][next_j] = false;
    }
  }
}


void wallsAndGates(vector< vector<int> >& matrix) {
  if(matrix.size() == 0 || matrix[0].size() == 0) return;
  vector< vector<bool> > used(matrix.size(), vector<bool>(matrix[0].size(), false));
  for(int i = 0; i < matrix.size(); ++i) {
    for(int j = 0; j < matrix[i].size(); ++j) {
      if((matrix[i][j] == INT_MAX) && (used[i][j] == false)) {
        used[i][j] = true;
        long long int distance = 0;
        long long int minDistance = INT_MAX;
        wallsAndGates(matrix, i, j, used, distance, minDistance);
        used[i][j] = false;
        matrix[i][j] = minDistance;
      }
    }
  }
}

DFS can make it go much faster!

Think that, we actually dont need to get all the minDistance. All the INT_MAX surrounds '0' will have all dep == 1. Thus, we can use dfs to get all the room distance to one gate. Suppose we have two gates, the other gate distance needs to be changed only if the current distance is smaller than the other one. 

void dfs(vector< vector<int> >& matrix, int i, int j, int dep) {
  if(i < 0 || i >= matrix.size() || j < 0 || j >= matrix[i].size() || matrix[i][j] < dep) {
    return;
  }
  matrix[i][j] = dep;
  dfs(matrix, i + 1, j, dep + 1);
  dfs(matrix, i - 1, j, dep + 1);
  dfs(matrix, i, j + 1, dep + 1);
  dfs(matrix, i, j - 1, dep + 1);
}


void wallsAndGatesII(vector< vector<int> >& matrix) {
  for(int i = 0; i < matrix.size(); ++i) {
    for(int j = 0; j < matrix[i].size(); ++j) {
      if(matrix[i][j] == 0) {
        dfs(matrix, i, j, 0);
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值