Premium Only Content

2257. Count Unguarded Cells in the Grid
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.
Example 1:
Input: m = 4, n = 6, guards = [[0,0],[1,1],[2,3]], walls = [[0,1],[2,2],[1,4]]
Output: 7
Explanation: The guarded and unguarded cells are shown in red and green respectively in the above diagram.
There are a total of 7 unguarded cells, so we return 7.
Example 2:
Input: m = 3, n = 3, guards = [[1,1]], walls = [[0,1],[1,0],[2,1],[1,2]]
Output: 4
Explanation: The unguarded cells are shown in green in the above diagram.
There are a total of 4 unguarded cells, so we return 4.
Constraints:
1 <= m, n <= 105
2 <= m * n <= 105
1 <= guards.length, walls.length <= 5 * 104
2 <= guards.length + walls.length <= m * n
guards[i].length == walls[j].length == 2
0 <= rowi, rowj < m
0 <= coli, colj < n
All the positions in guards and walls are unique.
class Solution {
public:
const int UNGUARDED = 0;
const int GUARDED = 1;
const int GUARD = 2;
const int WALL = 3;
int countUnguarded(int m, int n, vector<vector<int>>& guards,
vector<vector<int>>& walls) {
vector<vector<int>> grid(m, vector<int>(n, UNGUARDED));
// Mark guards' positions
for (const auto& guard : guards) {
grid[guard[0]][guard[1]] = GUARD;
}
// Mark walls' positions
for (const auto& wall : walls) {
grid[wall[0]][wall[1]] = WALL;
}
// Helper lambda to update visibility
auto updateCellVisibility = [&](int row, int col,
bool isGuardLineActive) -> bool {
if (grid[row][col] == GUARD) {
return true;
}
if (grid[row][col] == WALL) {
return false;
}
if (isGuardLineActive) {
grid[row][col] = GUARDED;
}
return isGuardLineActive;
};
// Horizontal passes
for (int row = 0; row < m; row++) {
bool isGuardLineActive = grid[row][0] == GUARD;
for (int col = 1; col < n; col++) {
isGuardLineActive =
updateCellVisibility(row, col, isGuardLineActive);
}
isGuardLineActive = grid[row][n - 1] == GUARD;
for (int col = n - 2; col >= 0; col--) {
isGuardLineActive =
updateCellVisibility(row, col, isGuardLineActive);
}
}
// Vertical passes
for (int col = 0; col < n; col++) {
bool isGuardLineActive = grid[0][col] == GUARD;
for (int row = 1; row < m; row++) {
isGuardLineActive =
updateCellVisibility(row, col, isGuardLineActive);
}
isGuardLineActive = grid[m - 1][col] == GUARD;
for (int row = m - 2; row >= 0; row--) {
isGuardLineActive =
updateCellVisibility(row, col, isGuardLineActive);
}
}
// Count unguarded cells
int count = 0;
for (int row = 0; row < m; row++) {
for (int col = 0; col < n; col++) {
if (grid[row][col] == UNGUARDED) {
count++;
}
}
}
return count;
}
};
-
14:13
Film Threat
1 day agoDISNEY SNOW WHITE SPOILER REVIEW | Film Threat Reviews
39.9K16 -
5:00
Kirill MultitoolOfficial
23 hours ago $1.38 earnedUseful Bushcraft skills and Lifehacks
26.6K1 -
11:18
ariellescarcella
22 hours agoWoke Judge Says : "No Prison Time For Trans Child Abuser"
22.5K26 -
8:11
BIG NEM
21 hours agoThe Balkan Wars, Israel-Palestine & Ukraine: A Cycle of Trauma
14.8K3 -
0:59
PewView
6 hours ago $0.39 earnedP09 c 60 second PewReview
10.7K3 -
5:45:48
Akademiks
16 hours agoDay 1/30. SAUCE WALKA SH*T? Yella Beezy Arrested for Mo3 M*RDER. FEDS DId A SWEEP in LA. YE INTERV?
124K35 -
49:27
The Connect: With Johnny Mitchell
1 day ago $25.59 earnedThe Truth About Mayo Zambada & The Fall Of The Sinaloa Drug Cartel- Mexico's Last Criminal EMPIRE
98.9K62 -
4:09:15
PeculiarPineTreePlays Minecraft Livestreams
21 hours ago $30.52 earnedTNT Blast Chamber Progress! - Shenanigang SMP Pancake Edition Ep46 - Minecraft Live Stream
139K6 -
3:02:56
Tundra Tactical
18 hours ago $12.07 earnedWhats The Deal With Suppressors?? Are They Protected By The Second Amendment?
78.8K16 -
4:03:38
Sgt Wilky Plays
19 hours agoHave some brewskis and playing some games
83.3K6