Answered step by step
Verified Expert Solution
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 northsouth or eastwest; 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 Water level and location of water: Current water level: W W Current water level: W WW Current water level: W WW Cannot flow, increasing water level to Current water level: W WW Cannot flow, increasing water level to Current water level: W WW W Current water level: W WW WW Current water level: W WW WW W Current water level: 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 test cases. Please debug it for test cases. test cases are not passing due to return status import numpy as np n intinput levels npfullnn visited npfullnn False for in rangen: levelsnparraylistmapint inputsplit center n stack centercenter bound set foundwall False def isvalidrowcol: if row or rown or col or coln: return False return True count def waterlevel: global stack, levels, bound, foundwall, count #printstack #printworking count count temp while stack: row, col stack.pop if levelsrowcollevel: levelsrowcol visitedrowcolTrue if row or rown or col or coln: foundwallTrue else: bound.addrowcol if levelsrowcol: if isvalidrowcol and not visitedrowcol: temp.appendrowcol if isvalidrowcol and not visitedrowcol: temp.appendrowcol if isvalidrowcol and not visitedrowcol: temp.appendrowcol if isvalidrowcol and not visitedrowcol: temp.appendrowcol stack temp if stack and not foundwall: waterlevel waterlevelscentercenter def iffound: global bound,stack if foundwall: return waterlevel else: lst for row,col in bound: lstappendlevelsrowcol level minlst stack listbound bound set waterlevel iffound iffound for i in rangen: for j in rangen: if levelsij: printWend else: printend print
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 northsouth or eastwest; 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
Water level and location of water:
Current water level:
W
W
Current water level:
W
WW
Current water level:
W
WW
Cannot flow, increasing water level to
Current water level:
W
WW
Cannot flow, increasing water level to
Current water level:
W
WW
W
Current water level:
W
WW
WW
Current water level:
W
WW
WW
W
Current water level: 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 test cases. Please debug it for test cases. test cases are not passing due to return status import numpy as np
n intinput
levels npfullnn
visited npfullnn False
for in rangen:
levelsnparraylistmapint inputsplit
center n
stack centercenter
bound set
foundwall False
def isvalidrowcol:
if row or rown or col or coln:
return False
return True
count
def waterlevel:
global stack, levels, bound, foundwall, count
#printstack
#printworking count
count
temp
while stack:
row, col stack.pop
if levelsrowcollevel:
levelsrowcol
visitedrowcolTrue
if row or rown or col or coln:
foundwallTrue
else:
bound.addrowcol
if levelsrowcol:
if isvalidrowcol and not visitedrowcol:
temp.appendrowcol
if isvalidrowcol and not visitedrowcol:
temp.appendrowcol
if isvalidrowcol and not visitedrowcol:
temp.appendrowcol
if isvalidrowcol and not visitedrowcol:
temp.appendrowcol
stack temp
if stack and not foundwall:
waterlevel
waterlevelscentercenter
def iffound:
global bound,stack
if foundwall:
return waterlevel
else:
lst
for row,col in bound:
lstappendlevelsrowcol
level minlst
stack listbound
bound set
waterlevel
iffound
iffound
for i in rangen:
for j in rangen:
if levelsij:
printWend
else:
printend
print
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started