-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathCount Unguarded Cells in the Grid.cpp
64 lines (49 loc) · 1.82 KB
/
Count Unguarded Cells in the Grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
Solution by Rahul Surana
***********************************************************
You are given two integers m and n representing a 0-indexed m x n grid.
You are also given two 2D integer arrays guards and walls where guards[i] = [rowi, coli] and
walls[j] = [rowj, colj] represent the positions of the ith guard and jth wall respectively.
A guard can see every cell in the four cardinal directions (north, east, south, or west) starting from their position unless obstructed by a wall or another guard.
A cell is guarded if there is at least one guard that can see it.
Return the number of unoccupied cells that are not guarded.
***********************************************************
*/
class Solution {
public:
void fill(vector<vector<int>> &vis, int x, int y, int m, int n){
for(int r = x-1; r>=0; r--){
if (vis[r][y] == 1 || vis[r][y] == 2) break;
vis[r][y] = 3;
}
for(int r = x+1; r<m; r++){
if (vis[r][y] == 1 || vis[r][y] == 2) break;
vis[r][y] = 3;
}
for(int r = y-1; r>=0; r--){
if (vis[x][r] == 1 || vis[x][r] == 2) break;
vis[x][r] = 3;
}
for(int r = y+1; r<n; r++){
if (vis[x][r] == 1 || vis[x][r] == 2) break;
vis[x][r] = 3;
}
}
int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
vector<vector<int>> vis(m,vector<int>(n,0));
int ans = 0;
for( auto &z: walls){
vis[z[0]][z[1]] = 1;
}
for( auto &z: guards){
vis[z[0]][z[1]] = 2;
}
for( auto &z: guards){
fill(vis,z[0],z[1],m,n);
}
for(auto &z: vis){
for(auto &x: z){ans+=(x ==0);}
}
return ans;
}
};