- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- 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 minimum distance that needs to be covered to meet all person in Python

Suppose we have a 2D matrix there are few values like below −

0 represents an empty cell.

1 represents a wall.

2 represents a person.

Here a person can walk any of these four directions (up, down, left and right). We have to find a cell that is not wall such that it minimizes the total travel distance each person has to walk to and finally find the distance.

So, if the input is like

2 | 0 | 1 | 0 |

1 | 0 | 1 | 2 |

0 | 0 | 2 | 2 |

then the output will be 7 as the best meeting point is the bottom right corner.

To solve this, we will follow these steps −

twos := a new map, costs := a new map

for each index i and row r in matrix, do

for each index j and value v in r, do

if v is same as 2, then

twos[i, j] := [i, j, 0]

costs[i, j] := make a 2D matrix of size as given matrix and fill with infinity

for each key value pair (k, q) in twos, do

seen := a new set

while q is not empty, do

(i, j, cost) := deleted first element from q

if (i, j) is in seen, then

go for the next iteration

add(i, j) into seen

costs[k, i, j] := cost

for each (di, dj) in ((1, 0), (−1, 0), (0, 1), (0, −1)), do

(ni, nj) := (i + di, j + dj)

if ni and nj are in range of matrix and matrix[ni, nj] is not 1, then

insert (ni, nj, cost + 1) at the end of q

ans := infinity

for i in range 0 to row count of matrix, do

for j in range 0 to column count of matrix, do

cur_cost := 0

for each arr in list of all values of costs, do

cur_cost := cur_cost + arr[i, j]

ans := minimum of ans and cur_cost

return ans

Let us see the following implementation to get better understanding −

## Example

class Solution: def solve(self, matrix): twos = {} costs = {} for i, r in enumerate(matrix): for j, v in enumerate(r): if v == 2: twos[(i, j)] = [(i, j, 0)] costs[(i, j)] = [[1e9 for _ in matrix[0]] for _ in matrix] for k, q in twos.items(): seen = set() while q: i, j, cost = q.pop(0) if (i, j) in seen: continue seen.add((i, j)) costs[k][i][j] = cost for di, dj in ((1, 0), (-1, 0), (0, 1), (0, -1)): ni, nj = i + di, j + dj if (ni >= 0 and nj >= 0 and ni < len(matrix) and nj < len(matrix[0]) and matrix[ni][nj] != 1): q.append((ni, nj, cost + 1)) ans = 1e9 for i in range(len(matrix)): for j in range(len(matrix[0])): cur_cost = 0 for arr in costs.values(): cur_cost += arr[i][j] ans = min(ans, cur_cost) return ans ob = Solution() matrix = [ [2, 0, 1, 0], [1, 0, 1, 2], [0, 0, 2, 2] ] print(ob.solve(matrix))

## Input

matrix = [ [2, 0, 1, 0], [1, 0, 1, 2], [0, 0, 2, 2]]

## Output

7

- Related Articles
- Program to find number of minimum steps required to meet all person at any cell in Python
- Program to find out number of blocks that can be covered in Python
- Program to find minimum amount needed to be paid all good performers in Python
- Program to find minimum distance to the target element using Python
- Program to find minimum time to complete all tasks in python
- Program to find minimum cost to connect all points in Python
- Program to find minimum time to finish all jobs in Python
- Program to Find Out the Minimum Cost to Purchase All in Python
- Program to count minimum k length sublist that can be flipped to make all items of list to 0 in Python
- Program to find minimum swaps needed to group all 1s together in Python
- Program to find out the minimum path to deliver all letters in Python
- Program to find minimum total distance between house and nearest mailbox in Python
- Find the distance covered to collect items at equal distances in Python
- Program to find minimum distance of two given words in a text in Python
- Program to find minimum bus fare for travelling all days in Python?