Question: def simulate _ water _ flow ( matrix ) : n = len ( matrix ) water _ level = matrix [ n / /

def simulate_water_flow(matrix):
n = len(matrix)
water_level = matrix[n //2][n //2]
while True:
for i in range(n):
for j in range(n):
if matrix[i][j]== water_level:
if i >0 and matrix[i -1][j]=='.':
matrix[i -1][j]='W'
if i < n -1 and matrix[i +1][j]=='.':
matrix[i +1][j]='W'
if j >0 and matrix[i][j -1]=='.':
matrix[i][j -1]='W'
if j < n -1 and matrix[i][j +1]=='.':
matrix[i][j +1]='W'
if all(cell !='.' for row in matrix for cell in row):
break
water_level +=1
return matrix
# Input reading
n = int(input())
terrain_matrix =[list(map(int, input().split())) for _ in range(n)]
# Call the simulation function
result_matrix = simulate_water_flow(terrain_matrix)
# Print the result
for row in result_matrix:
print(''.join(map(str, row)))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!