Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The problem is an over simplification of the flow of liquid. - A terrain is given as a grid of cells of random elevations. The

The problem is an over simplification of the flow of liquid. - A terrain is given as a grid of cells of random elevations. The grid is always odd sized and is always a square. - A liquid is poured at the central cell. Water can flow only north-south or east-west; not diagnonally. - At the first step, the water level is the same as the central cell. - Water from one cell flows to a neighbouring cell if the level of water is equal to greater than the elevation of the neighbouring cell. - When the water flows to the neighbouring cell, the level of water is maintained. - If the water cannot flow to any new cell, the water level rises. - The simulation stops when the water reaches the end of the domain. - The output consists of the domain represented by . and W representing dry and wet terrain.
Below is an example
Input Format
7494888977098472650734095922030163928029066690663282412750578767349984317219361315454421112460575572389635170174946593314300620167931780416954275
Water level and location of water:``
----------
Current water level: 172
.......
.......
.......
...W...
...W...
.......
.......
----------
Current water level: 172
.......
.......
.......
...W...
..WW...
.......
.......
----------
Current water level: 172
.......
.......
.......
...W...
..WW...
.......
.......
Cannot flow, increasing water level to 173
----------
Current water level: 173
.......
.......
.......
...W...
..WW...
.......
.......
Cannot flow, increasing water level to 174
----------
Current water level: 174
.......
.......
.......
...W...
..WW...
..W....
.......
----------
Current water level: 174
.......
.......
.......
...W...
..WW...
.WW....
.......
----------
Current water level: 174
.......
.......
.......
...W...
..WW...
.WW....
.W.....
----------
Current water level: 174 Reached edge, exiting. Solution:
Output Format
.......
.......
.......
...W...
..WW...
.WW....
.W.....
Constraints: First line of the input has dimension n of (n X n) matrix. Followed by the matrix itself as shown below in the samples . I will be attaching a python code which passes 6 test cases. Please debug it for 10 test cases. 3 test cases are not passing due to return status import numpy as np
n = int(input())
levels = np.full((n,n),0)
visited = np.full((n,n), False)
for _ in range(n):
levels[_]=np.array(list(map(int, input().split())))
center = n//2
stack =[(center,center)]
bound = set()
found_wall = False
def is_valid(row,col):
if row<=0 or row>=n or col<0 or col>=n:
return False
return True
count=0
def water(level):
global stack, levels, bound, found_wall, count
#print(stack)
#print("working", count)
count+=1
temp =[]
while stack:
row, col = stack.pop()
if levels[row][col]<=level:
levels[row][col]=-1
visited[row][col]=True
if row==0 or row==n-1 or col==0 or col==n-1:
found_wall=True
else:
bound.add((row,col))
if levels[row][col]==-1:
if is_valid(row+1,col) and not visited[row+1][col]:
temp.append((row+1,col))
if is_valid(row-1,col) and not visited[row-1][col]:
temp.append((row-1,col))
if is_valid(row,col-1) and not visited[row][col-1]:
temp.append((row,col-1))
if is_valid(row,col+1) and not visited[row][col+1]:
temp.append((row,col+1))
stack = temp
if stack and not found_wall:
water(level)
water(levels[center,center])
def if_found():
global bound,stack
if found_wall:
return water(level)
else:
lst =[]
for row,col in bound:
lst.append(levels[row][col])
level = min(lst)
stack = list(bound)
bound = set()
water(level)
if_found()
if_found()
for i in range(n):
for j in range(n):
if levels[i][j]==-1:
print("W",end="")
else:
print(".",end="")
print()

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

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

Identify some of the global differences when negotiating.

Answered: 1 week ago

Question

Describe the team performance model.

Answered: 1 week ago