问题
For example given the below matrix:
[[[0, 8], [0, 3], [0, 8]],
[[8, 0], [3, 0], [0, 5]],
[[0, 1], [0, 6], [0, 0]]]
where for each tuple the first number is food and the second number is water. I need to get from the bottom right to the top left and I can only move up or left.
I need to gather as much food and water as possible so I can survive as long as possible. For each day I want to survive I need 1 food and 1 water so if I could choose between a path that would result in (7,4) and one that would result in (6,6) the correct choice is (6,6) as that will allow me to live for 6 days.
How does one go about finding the best path through said matrix?
My current code is below however it does not work(it finds a very high cost path but not the highest) and I have no idea how to go about it. I have no idea how to begin implementing it, though I have been told to avoid recursion.
def maxSuppliesPath(matrix):
n = len(matrix) - 1
bestPath = matrix
# Initialize first column of bestPath array
for i in range(1, n + 1):
if bestPath[i][0] == 0:
bestPath[i][0] = bestPath[i - 1][0]
else:
bestPath[i][0] = (bestPath[i][0][0] + bestPath[i - 1][0][0], bestPath[i][0][1] + bestPath[i - 1][0][1])
# Initialize first row of bestPath array
for j in range(1, n + 1):
if bestPath[0][j] == 0:
bestPath[0][j] = bestPath[0][j - 1]
else:
bestPath[0][j] = (bestPath[0][j - 1][0] + bestPath[0][j][0], bestPath[0][j - 1][1] + bestPath[0][j][1])
# Construct rest of the bestPath array
for i in range(1, n + 1):
for j in range(1, n + 1):
if min(bestPath[i][j - 1][0] + bestPath[i][j][0], bestPath[i][j - 1][1] + bestPath[i][j][1]) > min(
bestPath[i - 1][j][0] + bestPath[i][j][0], bestPath[i - 1][j][1] + bestPath[i][j][1]):
bestPath[i][j] = (bestPath[i][j - 1][0] + bestPath[i][j][0], bestPath[i][j - 1][1] + bestPath[i][j][1])
else:
bestPath[i][j] = (bestPath[i - 1][j][0] + bestPath[i][j][0], bestPath[i - 1][j][1] + bestPath[i][j][1])
return min(bestPath[n][n][0], bestPath[n][n][1])
回答1:
The first step in solving the problem is to understand how to traverse the matrix. The image below shows the distance from the starting point to each other point.
Notice that equidistant points are arranged on a diagonal. Given a set
(call it A) that represents one diagonal, the points on the next diagonal (call it B) are found as follows:
for each point in set A
if the x coordinate is greater than zero
add (x-1, y) to set B
if the y coordinate is greater than zero
add (x, y-1) to set B
In the example, the sets representing the diagonals should look like this:
[(2, 2)] // starting position
[(1, 2), (2, 1)] // after 1 move
[(2, 0), (1, 1), (0, 2)] // after 2 moves
[(0, 1), (1, 0)] // after 3 moves
[(0, 0)] // after 4 moves
The image below shows how many different paths can be used to reach each point in the matrix. The number of paths is the same as the numbers in Pascal's triangle. For the purposes of this question, the only thing that matters is that the numbers grow quickly, so we need to reduce the count.
To reduce the path count, we need to cull non-productive paths as we traverse the matrix. This is accomplished by comparing tuples, where each tuple consists of food and water. A tuple (F,W)
dominates a tuple (f,w)
iff F >= f
AND W >= w
.
For example, consider the center position in the matrix. We can reach that point either by moving up-then-left, or by moving left-then-up. Moving up-then-left yields (food, water) of (3,5), whereas moving left-then-up yields (3,6). (3,6) dominates (3,5), so we only need to consider (3,6). So after two moves, we have the situation shown below. Note that we only have 1 tuple in the center position, rather than the two that Pascal's triangle would predict.
After three moves, we have the situation shown below. We have two tuples for each point on the third diagonal. That's necessary because one of the tuples has more food and the other has more water, so neither dominates the other.
After four moves, we have four possible answers, and choosing the best is a simple matter of comparing min(food, water)
for each of the tuples.
来源:https://stackoverflow.com/questions/62062622/optimal-path-through-a-matrix-with-multiple-costs-to-consider