- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find out the shortest path to reach the goal in Python

Suppose, we are given a grid where the cells contain various symbols such as 'X', 'O', '*' and '#' and the symbols have various meanings.

- '#' is the goal cell that we want to reach.
- 'O' is a free cell via which we can travel to the goal cell.
- '*' is our position in the cell.
- 'X' is a blocked cell, via which we cannot travel.

We have to find out the number of moves required to reach the goal cell from our current position in the grid. If the goal is not reachable, we return -1. The grid is given as input to the program.

So, if the input is like

X | X | O | X |

X | X | * | X |

X | # | O | X |

X | X | X | X |

then the output will be 2

To solve this, we will follow these steps −

- m := row count of grid
- n := column count of grid
- for i in range 0 to m, do
- for j in range 0 to n, do
- if grid[i, j] is same as "*", then
- come out from the loop

- otherwise,
- go for the next iteration

- come out from the loop

- if grid[i, j] is same as "*", then

- for j in range 0 to n, do
- ans := 0
- queue := a new list containing integer pair (i, j)
- grid[i, j] := "X"
- while queue is not empty, do
- ans := ans + 1
- newq := a new list
- for each i, j in queue, do
- for each ii, jj in(i-1, j) ,(i, j-1) ,(i, j+1) ,(i+1, j), do
- if 0 <= ii < m and 0 <= jj < n and grid[ii, jj] is not same as "X", then
- if grid[ii, jj] is same as "#", then
- return ans

- insert ii, jj at the end of newq
- grid[ii, jj] := "X"

- if grid[ii, jj] is same as "#", then

- if 0 <= ii < m and 0 <= jj < n and grid[ii, jj] is not same as "X", then

- for each ii, jj in(i-1, j) ,(i, j-1) ,(i, j+1) ,(i+1, j), do
- queue := newq

- return -1

## Example

Let us see the following implementation to get better understanding −

def solve(grid): m, n = len(grid), len(grid[0]) for i in range(m): for j in range(n): if grid[i][j] == "*": break else: continue break ans = 0 queue = [(i, j)] grid[i][j] = "X" while queue: ans += 1 newq = [] for i, j in queue: for ii, jj in (i-1, j), (i, j-1), (i, j+1), (i+1, j): if 0 <= ii < m and 0 <= jj < n and grid[ii][jj] != "X": if grid[ii][jj] == "#": return ans newq.append((ii, jj)) grid[ii][jj] = "X" queue = newq return -1 print(solve([['X', 'X', 'O', 'X'],['X', 'X', '*', 'X'],['X', '#', 'O', 'X'],['X', 'X', 'X', 'X']]))

## Input

[['X', 'X', 'O', 'X'], ['X', 'X', '*', 'X'], ['X', '#', 'O', 'X'], ['X', 'X', 'X', 'X']]

## Output

2

- Related Questions & Answers
- C++ program to find out the shortest cost path in a given graph for q queries
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- Program to find out the minimum path to deliver all letters in Python
- Program to find out the farthest building a parkour artist can reach in Python
- Program to find goal parser interpretation command in Python
- Program to find out the minimum cost path between the lowest value vertex to the highest value vertex (Python)
- Program to Find Out the Minimal Submatrices in Python
- Program to find out the path between two vertices in a graph that has the minimum penalty (Python)
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Program to find the resolved Unix style path in Python
- C++ Program to find out the sum of shortest cost paths for all given triplets
- Program to find out the efficient way to study in Python
- C++ Program to find the Shortest Distance to a character
- Program to Find Out the Edges that Disconnect the Graph in Python
- Program to Find Out the Strings of the Same Size in Python

Advertisements