# [LeetCode] 286. Walls and Gates 墙和门

2021年09月15日 阅读数：1

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

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`.java

For example, given the 2D grid:python

```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:app

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

给一个m*n 的二维方格，开始里面有-1表明墙或者障碍物，0表明门，INF 表明空房间。把空房间用离它最近的门的距离填充，若是没有能到达的门，则填充 INF。less

Java: DFSurl

```public void wallsAndGates(int[][] rooms) {
if(rooms==null || rooms.length==0||rooms[0].length==0)
return;

int m = rooms.length;
int n = rooms[0].length;

boolean[][] visited = new boolean[m][n];

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(rooms[i][j]==0){
fill(rooms, i-1, j, 0, visited);
fill(rooms, i, j+1, 0, visited);
fill(rooms, i+1, j, 0, visited);
fill(rooms, i, j-1, 0, visited);
visited = new boolean[m][n];
}
}
}
}

public void fill(int[][] rooms, int i, int j, int start, boolean[][] visited){
int m=rooms.length;
int n=rooms[0].length;

if(i<0||i>=m||j<0||j>=n||rooms[i][j]<=0||visited[i][j]){
return;
}

rooms[i][j] = Math.min(rooms[i][j], start+1);
visited[i][j]=true;

fill(rooms, i-1, j, start+1, visited);
fill(rooms, i, j+1, start+1, visited);
fill(rooms, i+1, j, start+1, visited);
fill(rooms, i, j-1, start+1, visited);

visited[i][j]=false;
}
```

Java: DFSrest

```public void wallsAndGates(int[][] rooms) {
if(rooms==null || rooms.length==0||rooms[0].length==0)
return;

int m = rooms.length;
int n = rooms[0].length;

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(rooms[i][j]==0){
fill(rooms, i, j, 0);
}
}
}
}

public void fill(int[][] rooms, int i, int j, int distance){
int m=rooms.length;
int n=rooms[0].length;

if(i<0||i>=m||j<0||j>=n||rooms[i][j]<distance){
return;
}

rooms[i][j] = distance;

fill(rooms, i-1, j, distance+1);
fill(rooms, i, j+1, distance+1);
fill(rooms, i+1, j, distance+1);
fill(rooms, i, j-1, distance+1);
}　```

Java: BFScode

```public void wallsAndGates(int[][] rooms) {
if(rooms==null || rooms.length==0||rooms[0].length==0)
return;

int m = rooms.length;
int n = rooms[0].length;

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(rooms[i][j]==0){
}
}
}

while(!queue.isEmpty()){

if(x>0 && rooms[x-1][y]==Integer.MAX_VALUE){
rooms[x-1][y]=rooms[x][y]+1;
}

if(x<m-1 && rooms[x+1][y]==Integer.MAX_VALUE){
rooms[x+1][y]=rooms[x][y]+1;
}

if(y>0 && rooms[x][y-1]==Integer.MAX_VALUE){
rooms[x][y-1]=rooms[x][y]+1;
}

if(y<n-1 && rooms[x][y+1]==Integer.MAX_VALUE){
rooms[x][y+1]=rooms[x][y]+1;
}
}
}　　```

Python:

```# Time:  O(m * n)
# Space: O(g)

from collections import deque

class Solution(object):
def wallsAndGates(self, rooms):
"""
:type rooms: List[List[int]]
:rtype: void Do not return anything, modify rooms in-place instead.
"""
INF = 2147483647
q = deque([(i, j) for i, row in enumerate(rooms) for j, r in enumerate(row) if not r])
while q:
(i, j) = q.popleft()
for I, J in (i+1, j), (i-1, j), (i, j+1), (i, j-1):
if 0 <= I < len(rooms) and 0 <= J < len(rooms[0]) and \
rooms[I][J] == INF:
rooms[I][J] = rooms[i][j] + 1
q.append((I, J))　```

C++: 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 + 1, j, 1);
dfs(rooms, i - 1, j, 1);
dfs(rooms, i, j + 1, 1);
dfs(rooms, i, j - 1, 1);
}
}
}
}
void dfs(vector<vector<int>> &rooms, int i, int j, int val) {
if (i < 0 || i >= rooms.size() || j < 0 || j >= rooms[i].size()) return;
if (rooms[i][j] > val) {
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);
}
}
};
```

C++: DFS II

```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);
}
};
```

C++: BFS

```class Solution {
public:
void wallsAndGates(vector<vector<int>>& rooms) {
queue<pair<int, int>> q;
vector<vector<int>> dirs{{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
for (int i = 0; i < rooms.size(); ++i) {
for (int j = 0; j < rooms[i].size(); ++j) {
if (rooms[i][j] == 0) q.push({i, j});
}
}
while (!q.empty()) {
int i = q.front().first, j = q.front().second; q.pop();
for (int k = 0; k < dirs.size(); ++k) {
int x = i + dirs[k][0], y = j + dirs[k][1];
if (x < 0 || x >= rooms.size() || y < 0 || y >= rooms[0].size() || rooms[x][y] < rooms[i][j] + 1) continue;
rooms[x][y] = rooms[i][j] + 1;
q.push({x, y});
}
}
}
};
```

[LeetCode] 317. Shortest Distance from All Buildings 建筑物的最短距离