Question
The assumptions from Q1 are: 1. The grid is square 2. The number of cells on the path is the length of the path including
The assumptions from Q1 are:
1. The grid is square
2. The number of cells on the path is the length of the path including the start and the goal cells.
3. It cannot travel diagonally on the grid.
This is the code given in Python 3:
def shortest_path_grid(grid, start, goal):
'''
Function that returns the length of the shortest path in a grid
that HAS obstacles represented by 1s. 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 where 0 represents free space and 1s obstacles)
: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],
[1,1,0],
[1,1,0]]
start, goal = (0,1), (2,2)
print(shortest_path_grid(grid, start, goal))
assert shortest_path_grid(grid, start, goal) == 4
grid = [[0,1],
[1,0]]
start, goal = (0, 0), (1,1)
print(shortest_path_grid(grid, start, goal))
assert shortest_path_grid(grid, start, goal) == -1
5) [10 pts] (Coding) R2-D2 navigates the grid thanks to you but is now facing a more hostile grid, one with obstacles represented by 1s. Use the same assumptions provided in Q1 and return the length of the shortest path avoiding obstacles. Return -1 if no such path exists. Hints: 1. Don't explore neighbors that don't exist (are beyond the grid's boundary) 2. Don't explore neighbors that are obstacles. 3. Your search ends when there is nothing to explore. 4. You will have to propagate the explored length so far. 5. Recollect CSCI 2270 Here are some examples. grid = [[0,0,0], [1,1,0], [1,1,0]] start, goal = (0,1), (2,2) Solution = 4 grid = [[0,1], [1,01] start, goal = (0, 0), (1,1) Solution = -1 ---> The R2-D2 can't travel diagonally and thus there doesn't exist any feasible path
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