Skip to main content

Count Unguarded Cells in the Grid

Problem Statement​

In this tutorial, we will solve the Count Unguarded Cells in the Grid problem . We will provide the implementation of the solution in Python, Java, and C++.

Problem Description​

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.

Examples​

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.

Solution of Given Problem​

Intuition and Approach​

The problem can be solved using a brute force approach or an optimized Technique.

Approach 1:Brute Force (Naive)​

Brute Force Approach: Simulates each guard's visibility across the entire grid, marking cells until obstacles are encountered. It then counts unguarded cells.

Codes in Different Languages​

Written by @AmruthaPariprolu
#include <vector>
#include <unordered_set>

using namespace std;

int countUnguarded(int m, int n, vector<vector<int>>& guards, vector<vector<int>>& walls) {
vector<vector<int>> grid(m, vector<int>(n, 0));

// Mark guards on the grid
for (auto& guard : guards) {
grid[guard[0]][guard[1]] = -1;
}

// Mark walls on the grid
for (auto& wall : walls) {
grid[wall[0]][wall[1]] = 1;
}

// Function to check if a cell is within bounds and unguarded
auto isUnguarded = [&](int x, int y) -> bool {
return x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0;
};

// Directions for scanning (north, east, south, west)
vector<pair<int, int>> directions = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

// Simulate visibility for each guard
for (auto& guard : guards) {
int x = guard[0], y = guard[1];
for (auto& dir : directions) {
int dx = dir.first, dy = dir.second;
int nx = x + dx, ny = y + dy;
while (isUnguarded(nx, ny)) {
grid[nx][ny] = 1;
nx += dx;
ny += dy;
}
}
}

// Count unguarded cells
int unguardedCount = 0;
for (int x = 0; x < m; ++x) {
for (int y = 0; y < n; ++y) {
if (grid[x][y] == 0) {
++unguardedCount;
}
}
}

return unguardedCount;
}

Complexity Analysis​

  • Time Complexity: O((g+w)∗(n+m))O((g+w)*(n+m))
  • Space Complexity: O(n∗m)O(n*m)
  • where:g is the number of guards,w is the number of walls,n is the number of rows in the grid,m is the number of columns in the grid.
  • The time complexity is O((g+w)∗(n+m))O((g+w)*(n+m)) .This complexity arises because each guard potentially scans in all four cardinal directions, and each scan operation could traverse up to n or m cells.
  • Additional space may be required for storing guards' and walls' positions, but this is typically bounded by O(g + w), where g is the number of guards and w is the number of walls.

Authors:

Loading...