0% found this document useful (0 votes)
3 views2 pages

Mark Unsafe Zones in 2D Matrix

Uploaded by

yATHARTH Tyagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Mark Unsafe Zones in 2D Matrix

Uploaded by

yATHARTH Tyagi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like