Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Implement all the given functions that are used to solve the below problems. Follow the path You are given a matrix of size m x
Implement all the given functions that are used to solve the below problems.
Follow the path
You are given a matrix of size m x n consisting of ones and zeros There is a single continuous path formed with ones that starts from the rightmost cell in the last row mth row with one and ends at leftmost cell in the first row with one in it The path does not branch, and there is only one such path. Your task is to traverse along the path and print the coordinates of the path from start to end as tuples over multiple lines. The path can move vertically and horizontally.
Input
matrix
Output
def indexoffirstoccurancerow:list,elem:
Given a list find the index of first occurance of in it
def indexoflastoccurancerow:list,elem:
Given a list find the index of last occurance of in it
Hint: use indexoffirstone with reversal.
def isvalidcoordinatex:int,y:int, M:
Checks if the xy is a valid corrdinateindices in the matrix Mlist of list Assume coordinates are nonnegative
def validadjacentcoordinatesx:int,y:int, M:
Create a set of valid adjacent coordinatesindices given xy and a matrix M
return
xy
for xy in # all the possible adjacent coordinates
if isvalidcoordinatexy M
def nextcoordinatewithvaluecurrcoords, value, M prevcoordsNone:
Find the coordinateindices of the next coordinate that has the value in it For the starting coordinate the prevcoords would be None
def getpathcoordinatesM:
Given the matrix m find the path formed by from the last row to the first row.
xstart, xend lenM
ystart, yend indexoflastoccuranceM indexoffirstoccuranceM
def printpathM:
path getpathcoordinatesM
def alternatepathM:
path getpathcoordinatesM
def countpathM:
path getpathcoordinatesM
def mirrorhorizontallyM:
path getpathcoordinatesM
def mirrorverticallyM:
path getpathcoordinatesM
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