Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find minimum cost to pick up gold in given two locations in Python
Suppose we have a 2D matrix and some other values like row, col, erow0, ecol0, erow1, and ecol1. If our current position is matrix[row, col] and we want to pick up gold that is at matrix[erow0, ecol0] and matrix[erow1, ecol1]. We can go up, down, left, and right but when we are at a cell (r, c), we have to pay cost matrix[r, c]. However, if we land at a cell more than once, we do not need to pay cost for that cell again. We have to find the minimum cost to pick up gold at both locations.
So, if the input is like:
| 1 | 1 | 1 | 1 | 1 |
| 1 | 10 | 10 | 10 | 10 |
| 1 | 1 | 1 | 10 | 10 |
row = 0, col = 0, erow0 = 0, ecol0 = 3, erow1 = 2, ecol1 = 2, then the output will be 8. We are at (0, 0) and want to pick gold from location (0, 3) and (2, 2). The optimal path visits cells with cost 1 to minimize the total cost.
Algorithm
To solve this problem, we use Dijkstra's algorithm to find the shortest path from three different starting points and then find the optimal meeting point ?
Define a function
is_valid()to check if coordinates are within matrix boundsDefine a function
min_cost()that uses Dijkstra's algorithm to find minimum cost from a starting position to all other cellsCalculate minimum costs from: starting position, first gold location, second gold location
Find the optimal meeting point that minimizes total cost
Implementation
import heapq
import math
class Solution:
def solve(self, matrix, row, col, erow0, ecol0, erow1, ecol1):
def is_valid(x, y):
return x >= 0 and y >= 0 and x < len(matrix) and y < len(matrix[0])
def min_cost(sx, sy):
heap = [(matrix[sx][sy], sx, sy)]
dists = [[math.inf] * len(matrix[0]) for _ in range(len(matrix))]
dists[sx][sy] = matrix[sx][sy]
while heap:
cost, x, y = heapq.heappop(heap)
for nx, ny in [(x, y - 1), (x + 1, y), (x - 1, y), (x, y + 1)]:
if is_valid(nx, ny) and matrix[nx][ny] + cost < dists[nx][ny]:
edge = matrix[nx][ny]
dists[nx][ny] = edge + cost
heapq.heappush(heap, (edge + cost, nx, ny))
return dists
res = math.inf
a, b, c = min_cost(row, col), min_cost(erow0, ecol0), min_cost(erow1, ecol1)
for i in range(len(matrix)):
for j in range(len(matrix[0])):
res = min(res, a[i][j] + b[i][j] + c[i][j] - 2 * matrix[i][j])
return res
# Test the solution
ob = Solution()
matrix = [
[1, 1, 1, 1, 1],
[1, 10, 10, 10, 10],
[1, 1, 1, 10, 10]
]
row = 0
col = 0
erow0 = 0
ecol0 = 3
erow1 = 2
ecol1 = 2
result = ob.solve(matrix, row, col, erow0, ecol0, erow1, ecol1)
print(result)
Output
8
How It Works
The algorithm works by:
Dijkstra's Algorithm: Used to find shortest paths from each of the three key positions (start, gold1, gold2)
Meeting Point Strategy: For each cell (i,j), calculate the total cost if all three paths meet at that cell
Cost Adjustment: Subtract
2 * matrix[i][j]because the meeting cell cost is counted three times but should only be paid onceOptimization: Find the cell that minimizes the total travel cost
Conclusion
This solution uses Dijkstra's algorithm to find the minimum cost path from three different starting points. The optimal strategy is to find a meeting point that minimizes the total cost of collecting both pieces of gold.
