Algorithm: Dynamic programming, two-dimensional matrix cost maximum value Advanced version Two travel paths, once intersect, solve the maximum cost

2D Matrix Advanced Problem: Solve the maximum total calorie consumption of two persons

problem:

There is an existing 2D matrix N x M, given a two-dimensional array calorie[N][M] represents the calorie value consumed when accessing the coordinates (N, M). Now there are two people, one (boy) starts at (1, 1), the other (girl) starts at (N, 1), they have to reach the end 1 (N, M) and end 2 (1, M) respectively . Boys can only move to the right or downwards with one step, and girls can only move to the right or upwards with one step. And on the path of the two, only one meeting point is allowed, and in the total calorie consumption, the calories of this meeting point are not added to the consumption of boys or girls, and the total calorie consumption is calculated.

Insert picture description here

problem analysis:

At first glance, this question is much more complicated than our previous three questions. But apart from various conditions, it is essentially a problem of the most cost-effective solution. Next, we focus on analyzing the key constraints in the article: only one point of intersection is allowed on the path of the two.

Suppose the meeting point is (i, j)

First, the possible paths before and after the boy passes through this meeting point are:

  • (i-1, j) --> (i, j) --> (i+1, j)
  • (i-1, j) --> (i, j) --> (i, j+1)
  • (i, j-1) --> (i, j) --> (i, j+1)
  • (i, j-1) --> (i, j) --> (i+1, j)

Similarly, the possible paths before and after the girl passes this meeting point are:

  • (i+1, j) --> (i, j) --> (i-1, j)
  • (i+1, j) --> (i, j) --> (i, j+1)
  • (i, j-1) --> (i, j) --> (i-1, j)
  • (i, j-1) --> (i, j) --> (i, j+1)

Since there can be no other same travel coordinates except for the meeting point (i, j), the possible paths before and after the two passing through the meeting point can only be:

  • 男孩:(i-1, j) --> (i, j) --> (i+1, j) ,女孩:(i, j-1) --> (i, j) --> (i, j+1)
  • 男孩:(i, j-1) --> (i, j) --> (i, j+1) ,女孩:(i+1, j) --> (i, j) --> (i-1, j)

In this blog post , an algorithm for the best value of the cost to reach a certain coordinate is solved. Can this algorithm be used to find the best value under the conditions? The answer is no. Because the maximum value array obtained by this algorithm is the minimum cost from the starting point (0, 0) to the target coordinates (N, M). It is not restricted to the point of meeting (i, j) that must be passed in the path. Therefore, the current problem needs to be divided into smaller problems

The path of the two is divided into four parts:

  • The boy arrives from the starting point before the meeting point
  • The boy left the meeting point and reached the end
  • Girl from the starting point to before the meeting point
  • The girl left the meeting point and reached the end

Just ask for

  • The cost of boys from (1, 1) to (i-1, j), (i+1, j) to (N, M) is the best value for girls from (N, 1) to (i, j-1), The sum of the most value of the cost from (i, j+1) to (1, M)
  • The cost of boys from (1, 1) to (i, j-1), (i, j+1) to (N, M), and girls from (N, 1) to (i+1, j), The sum of the most value of the cost from (i-1, j) to (1, M)

Then compare the two and take the best value to get the answer.

That is, it is necessary to request the most valuable array of path costs from the starting point of the boy and the girl to the meeting point, and the most valuable array of path costs from the meeting point to the end point, a total of four cost arrays.

There are some small details:

  • According to i-1, i+1, j-1, j+1 need to fall within the coordinate interval, we can get the meeting point (i, j) 2 <= i <= N-1, 2 <= j <= M- 1
The algorithm is as follows:
/**
 * 
 * @param {int[N][M]} calorie
 * @param {int} N
 * @param {int} M
 */
var calories = function(calorie, N, M) {
    
    
  var totalCalories = 0
  var BoyStartToMeet = construct(N, M)
  var BoyEndToMeet = construct(N,M)
  var GirlStartToMeet = construct(N, M)
  var GirlEndToMeet = construct(N, M)
  for (let i = 1; i <= N; i++) {
    
    
    for (let j = 1; j <= M; j++) {
    
    
      BoyStartToMeet[i][j] = Math.max(BoyStartToMeet[i - 1][j], BoyStartToMeet[i][j - 1]) + calorie[i][j]
    }
  }
  for (let i = N; i >= 1; i--) {
    
    
    for (let j = M; j >= 1; j--) {
    
    
      BoyEndToMeet[i][j] = Math.max(BoyEndToMeet[i + 1][j], BoyStartToMeet[i][j + 1]) + calorie[i][j]
    }
  }
  for (let i = N; i <= 1; i++) {
    
    
    for (let j = 1; j <= M; j++) {
    
    
      GirlStartToMeet[i][j] = Math.max(BoyStartToMeet[i + 1][j], BoyStartToMeet[i][j - 1]) + calorie[i][j]
    }
  }
  for (let i = 1; i <= N; i++) {
    
    
    for (let j = M; j >= 1; j--) {
    
    
      GirlEndToMeet[i][j] = Math.max(BoyEndToMeet[i - 1][j], BoyStartToMeet[i][j + 1]) + calorie[i][j]
    }
  }
  for (let i = 2; i < N; i++) {
    
    
    for (let j = 2; j < M; j++) {
    
    
      var opt1 = BoyStartToMeet[i][j - 1] + BoyEndToMeet[i][j + 1] + GirlStartToMeet[i + 1][j] + GirlEndToMeet[i - 1][j]
      var opt2 = BoyStartToMeet[i - 1][j] + BoyEndToMeet[i + 1][j] + GirlStartToMeet[i][j - 1] + GirlEndToMeet[i][j + 1]
      totalCalories = Math.max(totalCalories,Math.max(opt1,opt2))
    }
  }

  return totalCalories
}

var construct = function(N, M) {
    
    
  var c = new Array()
  for (let i = 0; i < N; i++) {
    
    
    c[i] = new Array()
    for (let j = 0; j < M; j++) {
    
    
      c[i][j] = 0          
    }
  }
  return c
}

If you have any questions or puzzles about the code in the solution, please comment and I will try my best to answer it for you.

Guess you like

Origin blog.csdn.net/qq_35714301/article/details/113688286