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;
}
};
-
1:58:02
Barry Cunningham
9 hours agoJIMMY KIMMEL CANCELLED | OBAMA IS WHINING! | JD VANCE ON JESSE WATTERS!
116K133 -
2:34:46
TheSaltyCracker
9 hours agoWe Got Him Fired ReeEEStream 9-17-25
164K388 -
43:44
Man in America
11 hours agoAmericans Are About to Lose Everything—And They Don’t Even Know It
67.5K33 -
1:41:11
Adam Does Movies
2 days ago $4.50 earnedTalking Movies + Ask Me Anything - LIVE
49.7K2 -
3:40:08
I_Came_With_Fire_Podcast
17 hours agoNASA Blocks China, TPUSA BOOSTED, Chinese Spamoflauge, & Factional Division
47.5K6 -
33:40
Jamie Kennedy
9 hours agoEp 222 Processing the Loss of Charlie Kirk | HTBITY with Jamie Kennedy
66.4K28 -
1:32:05
Badlands Media
1 day agoAltered State S3 Ep. 46: Tactical Nukes, Thermite, and the 9/11 Puzzle
79K12 -
9:18
ARFCOM News
14 hours ago $2.00 earnedNSSF "Celebrates" ATF Partnership | Glocks BANNED | Redundant Spooky Boi Ban
41.4K10 -
13:09:13
LFA TV
22 hours agoLFA TV ALL DAY STREAM - WEDNESDAY 9/17/25
310K61 -
1:00:00
BEK TV
1 day agoAPRIL LUND: FAITH, FOCUS, AND THE ROAD TO THE 2028 OLYMPIC MARATHON
26.6K1