Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ [Link].27
PROBLEM STATEMENT 12 : You're given a 2D matrix where:
• 1 = Nuclear leak source (dangerous zone)
• 0 = Safe area
• You need to mark all directly adjacent cells (top, bottom, left, right) of a 1 as unsafe
by changing them to -1.
This simulates how danger spreads from the source to nearby zones.
Input:
area = [
[0, 0, 0],
[0, 1, 0],
[0, 0, 0]
]
Output:
[
[0, -1, 0],
[-1, 1, -1],
[0, -1, 0]
]
CODE :
def mark(mat):
rows, cols = len(mat), len(mat[0])
danger = [(i, j) for i in range(rows) for j in range(cols) if mat[i][j] == 1]
for i, j in danger:
if i-1 >= 0 and mat[i-1][j] == 0:
mat[i-1][j] = -1
if i+1 < rows and mat[i+1][j] == 0:
mat[i+1][j] = -1
if j-1 >= 0 and mat[i][j-1] == 0:
mat[i][j-1] = -1
if j+1 < cols and mat[i][j+1] == 0:
mat[i][j+1] = -1
return mat
mat = [
24
Gaurav Satyawali MCA AI & DS) Sec-A
25210820077/ [Link].27
[0, 1, 0],
[0, 1, 0],
[0, 0, 0]
]
result = mark(mat)
for row in result:
print(row)
OUTPUT :
25