
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Find the minimum number of moves needed to move from one cell of matrix to another in Python
Suppose we have one N X N matrix M, and this is filled with 1, 0, 2, 3, We have to find the minimum numbers of moves required to move from source cell to destination cell. While visiting through blank cells only, we can visit up, down, right and left.
Cell with value 1 indicates Source.
Cell with value 2 indicates Destination.
Cell with value 3 indicates Blank cell.
Cell with value 0 indicates Blank Wall.
There will be only one source and only one destination cells. There may be more than one path to reach destination from source cell. Now, each move in matrix we consider as '1'.
So, if the input is like
3 | 3 | 1 | 0 |
3 | 0 | 3 | 3 |
3 | 3 | 0 | 3 |
0 | 3 | 2 | 3 |
then the output will be 5,
3 | 3 | 1 | 0 |
3 | 0 | 3 | 3 |
3 | 3 | 0 | 3 |
0 | 3 | 2 | 3 |
From start to destination the green path is shortest.
To solve this, we will follow these steps −
nodes := order * order + 2
g := a blank graph with ‘nodes’ number of vertices
k := 1
for i in range 0 to order, do
for j in range 0 to order, do
if mat[i, j] is not same as 0, then
if is_ok (i , j + 1 , mat) is non-zero, then
create an edge between k and k + 1 nodes of g
if is_ok (i , j - 1 , mat) is non-zero, then
create an edge between k, k - 1 nodes of g
if j < order - 1 and is_ok (i + 1 , j , mat) is non-zero, then
create an edge between k, k + order nodes of g
if i > 0 and is_ok (i - 1 , j , mat) is non-zero, then
create an edge between k, k - order nodes of g
if mat[i, j] is same as 1, then
src := k
if mat[i, j] is same as 2, then
dest := k
k := k + 1
return perform bfs from src to dest of g
Example
Let us see the following implementation to get better understanding −
class Graph: def __init__(self, nodes): self.nodes = nodes self.adj = [[] for i in range(nodes)] def insert_edge (self, src , dest): self.adj[src].append(dest) self.adj[dest].append(src) def BFS(self, src, dest): if (src == dest): return 0 level = [-1] * self.nodes queue = [] level[src] = 0 queue.append(src) while (len(queue) != 0): src = queue.pop() i = 0 while i < len(self.adj[src]): if (level[self.adj[src][i]] < 0 or level[self.adj[src][i]] > level[src] + 1 ): level[self.adj[src][i]] = level[src] + 1 queue.append(self.adj[src][i]) i += 1 return level[dest] def is_ok(i, j, mat): global order if ((i < 0 or i >= order) or (j < 0 or j >= order ) or mat[i][j] == 0): return False return True def get_min_math(mat): global order src , dest = None, None nodes = order * order + 2 g = Graph(nodes) k = 1 for i in range(order): for j in range(order): if (mat[i][j] != 0): if (is_ok (i , j + 1 , mat)): g.insert_edge (k , k + 1) if (is_ok (i , j - 1 , mat)): g.insert_edge (k , k - 1) if (j < order - 1 and is_ok (i + 1 , j , mat)): g.insert_edge (k , k + order) if (i > 0 and is_ok (i - 1 , j , mat)): g.insert_edge (k , k - order) if(mat[i][j] == 1): src = k if (mat[i][j] == 2): dest = k k += 1 return g.BFS (src, dest) order = 4 mat = [[3,3,1,0], [3,0,3,3], [3,3,0,3], [0,3,2,3]] print(get_min_math(mat))
Input
[[3,3,1,0], [3,0,3,3], [3,3,0,3], [0,3,2,3]]
Output
0
- Related Articles
- C++ program to find minimum number of steps needed to move from start to end
- Minimum number of moves to escape maze matrix in Python
- Program to find minimum number of operations required to make one number to another in Python
- Count minimum number of “move-to-front” moves to sort an array in C++
- Program to find minimum number of rocketships needed for rescue in Python
- C++ Program to find out the maximum number of moves to reach a unblocked cell to another unblocked cell in a grid
- Find the minimum number of preprocess moves required to make two strings equal in Python
- How to move a file from one folder to another using Python?
- How to Copy Cell Format from One Cell (One Sheet) to Another Cell (Sheet) in Excel?
- Program to find minimum number of operations to move all balls to each box in Python
- Move rows from one table to another in MySQL?
- Program to check minimum number of characters needed to make string palindrome in Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Program to find minimum jump needed to return from a folder to home in Python
- Find the minimum number of rectangles left after inserting one into another in C++
