# Walls and Gates in C++

C++Server Side ProgrammingProgramming

Suppose we have one m x n 2D grid, and that is initialized with these three possible values.

• -1 for a wall or an obstacle.

• 0 for a gate.

• INF This is infinity means an empty room.

Here 2^31 - 1 = 2147483647 is INF as we 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.

So, if the input is like

 inf -1 0 inf inf inf inf -1 inf -1 inf -1 0 -1 inf inf

then the output will be

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

To solve this, we will follow these steps −

• Define an array dir of size: 4 x 2 := {{1, 0}, {-1, 0}, {0, 1}, {0,-1}}

• n := size of rooms

• m := (if n is non-zero, then column count, otherwise 0)

• Define one queue q of pairs

• for initialize i := 0, when i < n, update (increase i by 1), do −

• for initialize j := 0, when j < m, update (increase j by 1), do −

• if rooms[i, j] is same as 0, then −

• insert { i, j } into q

• for initialize lvl := 1, when not q is empty, update (increase lvl by 1), do −

• sz := size of q

• while sz is non-zero, decrease sz by 1 in each iteration, do −

• Define one pair curr := first element of q

• delete element from q

• x := curr.first

• y := curr.second

• for initialize i := 0, when i < 4, update (increase i by 1), do −

• nx := x + dir[i, 0]

• ny := y + dir[i, 1]

• if nx < 0 or ny < 0 or nx >= n or ny >= m or rooms[nx, ny] < lvl, then −

• rooms[nx, ny] := lvl

• insert { nx, ny } into q

## Example

Let us see the following implementation to get better understanding −

Live Demo

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<vector<auto< > v){
cout << "[";
for(int i = 0; i<v.size(); i++){
cout << "[";
for(int j = 0; j <v[i].size(); j++){
cout << v[i][j] << ", ";
}
cout << "],";
}
cout << "]"<<endl;
}
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
class Solution {
public:
void wallsAndGates(vector<vector<int<>& rooms) {
int n = rooms.size();
int m = n ? rooms[0].size() : 0;
queue<pair<int, int> > q;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (rooms[i][j] == 0)
q.push({ i, j });
}
}
for (int lvl = 1; !q.empty(); lvl++) {
int sz = q.size();
while (sz--) {
pair<int, int> curr = q.front();
q.pop();
int x = curr.first;
int y = curr.second;
for (int i = 0; i < 4; i++) {
int nx = x + dir[i][0];
int ny = y + dir[i][1];
if (nx < 0 || ny < 0 || nx >= n || ny >= m || rooms[nx][ny] < lvl)
continue;
rooms[nx][ny] = lvl;
q.push({ nx, ny });
}
}
}
}
};
main(){
vector<vector<int<> v = {{2147483647,-1,0,2147483647}, {2147483647,2147483647,2147483647,-1}, {2147483647,-1,2147483647,-1}, {0,-1,2147483647,2147483647}};
Solution ob;
ob.wallsAndGates(v);
print_vector(v);
}

## Input

{{2147483647,-1,0,2147483647},{2147483647,2147483647,2147483647,-1}, {2147483647,-1,2147483647,-1},{0,-1,2147483647,2147483647}}

## Output

[[3, -1, 0, 1, ],[2, 2, 1, -1, ],[1, -1, 2, -1, ],[0, -1, 3, 4, ],]
Published on 18-Nov-2020 11:56:19