Question: (I need help with these problems; an explanation would be appreciated. In Python, please, thank you.) NASAs Perseverance is on Mars and has mapped out

(I need help with these problems; an explanation would be appreciated. In Python, please, thank you.)

NASAs Perseverance is on Mars and has mapped out an obstacle free square sector with grids. The free space is represented with 0s. Help It save precious energy by figuring out the shortest path from a given start position on this grid to a goal position. It cannot travel diagonally because of some issues. Assume that the grid is square and the number of cells on the path is the length of the path including the start and the goal cells. Complete the provided python function. Do not forget to add comments to your code. Here is an example -

grid = [[0,0,0],

[0,0,0],

[0,0,0]

start, goal = (0,0), (2,2)

Solution = 5

Note: Do not return the shortest path but just a single integer representing the length of the shortest path.

#####CODE

def shortest_path_grid(grid, start, goal):

'''

Function that returns the length of the shortest path in a grid

that has no obstacles. The length is simply the number of cells

on the path including the 'start' and the 'goal'

:param grid: list of lists (represents a square grid)

:param start: tuple of start index

:param goal: tuple of goal index

:return: length of path

'''

n = len(grid)

if __name__ == "__main__":

grid = [[0,0,0],

[0,0,0],

[0,0,0]]

start, goal = (0,0), (2,2)

print(shortest_path_grid(grid, start, goal))

assert shortest_path_grid(grid, start, goal) == 5

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!