- 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

# Minimum number of moves to escape maze matrix in Python

Suppose we have a binary matrix, where 0 is representing an empty cell, and 1 is a wall. If we start from top left corner (0, 0), we have to find the minimum number of cells it would take to get to bottom right corner (R-1, C-1) Here R is number of rows and C is number of columns. If we cannot find any answer, return -1.

So, if the input is like

0 | 0 | 0 | 1 | 0 |

0 | 0 | 1 | 1 | 0 |

0 | 0 | 0 | 1 | 1 |

1 | 1 | 0 | 0 | 0 |

then the output will be 8 because, we can select path like −

0 | 0 | 0 | 1 | 0 |

0 | 0 | 1 | 1 | 0 |

0 | 0 | 0 | 1 | 1 |

1 | 1 | 0 | 0 | 0 |

To solve this, we will follow these steps −

- R := number of rows
- C := number of columns
- q := an empty list, initially insert (0, 0, 1) if matrix[0, 0] is 0
- matrix[0, 0] := 1
- for each triplet (r, c, d) in q, do
- if (r, c) is same as (R - 1, C - 1), then
- return d

- for each x, y in [(r + 1, c) ,(r - 1, c) ,(r, c + 1) ,(r, c - 1) ], do
- if 0 <= x < R and 0 <= y < C and matrix[x, y] is same as 0, then
- matrix[x, y] := 1
- insert triplet (x, y, d + 1) at the end of q

- if 0 <= x < R and 0 <= y < C and matrix[x, y] is same as 0, then

- if (r, c) is same as (R - 1, C - 1), then
- return -1

## Example

Let us see the following implementation to get better understanding −

def solve(matrix): R, C = len(matrix), len(matrix[0]) q = [(0, 0, 1)] if not matrix[0][0] else [] matrix[0][0] = 1 for r, c, d in q: if (r, c) == (R - 1, C - 1): return d for x, y in [(r + 1, c), (r - 1, c), (r, c + 1), (r, c - 1)]: if 0 <= x < R and 0 <= y < C and matrix[x][y] == 0: matrix[x][y] = 1 q.append((x, y, d + 1)) return -1 matrix = [ [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 1], [1, 1, 0, 0, 0] ] print(solve(matrix))

## Input

[ [0, 0, 0, 1, 0], [0, 0, 1, 1, 0], [0, 0, 0, 1, 1], [1, 1, 0, 0, 0] ]

## Output

8

- Related Questions & Answers
- Escape a Large Maze Python
- Find the minimum number of moves needed to move from one cell of matrix to another in Python
- Minimum number of moves to make all elements equal using C++.
- Find the minimum number of preprocess moves required to make two strings equal in Python
- Minimum Moves to Equal Array Elements II in Python
- Minimum Knight Moves in C++
- Minimum Number of Flips to Convert Binary Matrix to Zero Matrix in C++
- Program to find minimum moves to make array complementary in Python
- Minimum number of given moves required to make N divisible by 25 using C++.
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Count minimum number of “move-to-front” moves to sort an array in C++
- Minimum Moves to Equal Array Elements in C++
- Escape Characters in Python
- Program to find number of moves to win deleting repeated integer game in Python
- Count number of ways to reach destination in a Maze in C++

Advertisements