Program to find minimum distance that needs to be covered to meet all person in Python

PythonServer Side ProgrammingProgramming

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

2010
1012
0022

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

 Live Demo

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
raja
Published on 21-Oct-2020 15:37:22
Advertisements