Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions

Question

How flying airoplane?

Answered: 1 week ago