Check if all enemies are killed with bombs placed in a matrix in Python

In this problem, we need to determine if all enemies in a matrix can be eliminated by bombs. The matrix contains three types of cells:

  • 0 for empty area
  • 1 for bomb
  • 2 for enemies

Bombs can blast horizontally and vertically across the entire row and column, destroying all enemies in their path (but not other bombs).

Problem Example

Consider this matrix:

0 0 2 0
0 1 0 0
0 2 0 0
0 0 1 0

The bomb at position [1,1] can eliminate the enemy at [2,1], and the bomb at [3,2] can eliminate the enemy at [0,2]. Since all enemies are covered, the result is True.

Algorithm

The approach involves two phases:

  • Phase 1: For each bomb, clear all enemies in its row and column by setting them to 0
  • Phase 2: Check if any enemies (value 2) remain in the matrix

Implementation

def solve(mat):
    r = len(mat)
    c = len(mat[0])
    
    # Phase 1: For each bomb, clear its row and column
    for i in range(r):
        for j in range(c):
            if mat[i][j] == 1:  # Found a bomb
                # Clear entire row (except other bombs)
                for x in range(r):
                    if mat[x][j] != 1:
                        mat[x][j] = 0
                # Clear entire column (except other bombs)
                for y in range(c):
                    if mat[i][y] != 1:
                        mat[i][y] = 0
    
    # Phase 2: Check if any enemies remain
    for i in range(r):
        for j in range(c):
            if mat[i][j] == 2:
                return False
    
    return True

# Test the function
matrix = [
    [0, 0, 2, 0],
    [0, 1, 0, 0],
    [0, 2, 0, 0],
    [0, 0, 1, 0]
]

print("Matrix:")
for row in matrix:
    print(row)
    
result = solve(matrix)
print(f"\nAll enemies eliminated: {result}")
Matrix:
[0, 0, 2, 0]
[0, 1, 0, 0]
[0, 2, 0, 0]
[0, 0, 1, 0]

All enemies eliminated: True

How It Works

The algorithm simulates bomb explosions:

  • When a bomb is found at position (i,j), it destroys all enemies in row i and column j
  • Other bombs are not destroyed by explosions
  • After all explosions, we check if any enemies survive

Time and Space Complexity

Complexity Value Explanation
Time O(r × c × (r + c)) For each cell, we may clear entire row and column
Space O(1) In-place modification of the matrix

Alternative Test Case

# Case where not all enemies can be eliminated
matrix2 = [
    [2, 0, 0],
    [0, 0, 0], 
    [0, 0, 1]
]

print("Matrix 2:")
for row in matrix2:
    print(row)

result2 = solve(matrix2)
print(f"\nAll enemies eliminated: {result2}")
Matrix 2:
[2, 0, 0]
[0, 0, 0]
[0, 0, 1]

All enemies eliminated: False

Conclusion

This algorithm efficiently determines if all enemies can be eliminated by simulating bomb explosions. The key insight is that bombs destroy entire rows and columns, making it possible to clear enemies across the matrix.

Updated on: 2026-03-25T14:14:02+05:30

221 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements