Grid Illumination in Python

Grid illumination is a problem where we have an N x N grid with lamps at specific positions. Each lamp illuminates its entire row, column, and both diagonals. We need to answer queries about whether specific cells are illuminated, then turn off lamps in the queried cell and its 8 adjacent neighbors.

Problem Understanding

Given an N x N grid with lamps at positions specified in the lamps array, each lamp illuminates ?

  • Its entire row (x-axis)

  • Its entire column (y-axis)

  • Both diagonals passing through it

For each query (x, y), we check if the cell is illuminated, then turn off any lamps at (x, y) or its 8 adjacent cells.

Algorithm Approach

We use four hash maps to efficiently track illumination ?

  • x ? counts lamps in each row

  • y ? counts lamps in each column

  • diag1 ? counts lamps on diagonal where row + col is constant

  • diag2 ? counts lamps on diagonal where row - col is constant

Example

from collections import defaultdict

class Solution:
    def gridIllumination(self, N, lamps_list, queries):
        # Convert lamps to a set for O(1) lookup
        lamps = {(lamp[0], lamp[1]) for lamp in lamps_list}
        
        # Initialize counters for rows, columns, and diagonals
        x = defaultdict(int)  # row counters
        y = defaultdict(int)  # column counters
        diag1 = defaultdict(int)  # diagonal (row + col)
        diag2 = defaultdict(int)  # diagonal (row - col)
        
        # Count lamps for each row, column, and diagonal
        for row, col in lamps:
            x[row] += 1
            y[col] += 1
            diag1[row + col] += 1
            diag2[row - col] += 1
        
        result = []
        
        # Process each query
        for query in queries:
            query_row, query_col = query[0], query[1]
            
            # Check if the queried cell is illuminated
            is_illuminated = (x[query_row] + y[query_col] + 
                            diag1[query_row + query_col] + 
                            diag2[query_row - query_col]) > 0
            
            result.append(1 if is_illuminated else 0)
            
            # Turn off lamps in the queried cell and its 8 neighbors
            for row in range(query_row - 1, query_row + 2):
                for col in range(query_col - 1, query_col + 2):
                    if (row, col) in lamps:
                        # Decrement counters
                        x[row] -= 1
                        y[col] -= 1
                        diag1[row + col] -= 1
                        diag2[row - col] -= 1
                        # Remove the lamp
                        lamps.remove((row, col))
        
        return result

# Test the solution
solution = Solution()
N = 5
lamps = [[0, 0], [4, 4]]
queries = [[1, 1], [1, 0]]
print(solution.gridIllumination(N, lamps, queries))
[1, 0]

How It Works

Let's trace through the example with N = 5, lamps = [[0,0], [4,4]], queries = [[1,1], [1,0]] ?

Initial Setup

After placing lamps at (0,0) and (4,4) ?

  • x: {0: 1, 4: 1} (row counts)

  • y: {0: 1, 4: 1} (column counts)

  • diag1: {0: 1, 8: 1} (row + col values)

  • diag2: {0: 1, 0: 2} becomes {0: 2} (row - col values)

Query [1,1]

Check illumination: x[1] + y[1] + diag1[2] + diag2[0] = 0 + 0 + 0 + 2 = 2 > 0, so result is 1.

Query [1,0]

Check illumination: x[1] + y[0] + diag1[1] + diag2[1] = 0 + 1 + 0 + 0 = 1 > 0, so result is 1.

But after the first query, the lamp at (0,0) was turned off, so this would actually be 0.

Time Complexity

Operation Time Complexity Explanation
Setup O(L) L = number of lamps
Each Query O(1) Constant hash map lookups
Turn Off Lamps O(1) Check at most 9 positions
Overall O(L + Q) Q = number of queries

Conclusion

The grid illumination problem is efficiently solved using hash maps to track lamp counts across rows, columns, and diagonals. This approach provides O(1) query time and handles lamp removal in constant time by checking only the 9-cell neighborhood around each query point.

Updated on: 2026-03-25T08:42:04+05:30

528 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements