Dynamic planning problem-------Lanqiao Cup real problem-------Blue Bridge Cup preparation

Digital triangle

image description

The figure above shows a digital triangle. There are many different paths from the top to the bottom of the triangle. For each path, add up the numbers on the path to get a sum. Your task is to find the largest sum.

Each step on the path can only go from one number to the number on the left or the right of the next level closest to it. In addition, the difference between the number of times to go down left and the number of times to go down right cannot exceed one .

Enter description

The first line of input contains an integer N (1≤ N ≤100), which represents the number of triangles.

The following N N lines give digital triangles. The numbers on the digital triangle are all integers between 0 and 100.

Output description

Output an integer to indicate the answer.

Sample input and output

Example

enter

5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5

Output

27

Operating restrictions

  • Maximum running time: 1s
  • Maximum operating memory: 256M

If we didn’t remind us to use dynamic programming to do what we would do at the beginning, we can definitely think of using a two-dimensional array to store our digital triangles. For clarity, I will draw it more intuitively. (Represented by a tree)

Insert picture description here

Ideas

According to the meaning of the question, we are going to calculate the largest value from the above. At first, I wanted to use an auxiliary array to store the values ​​and add up to see the largest one, but that would be too troublesome. We can directly modify our numbers. Triangle to get our path value

The question is, can the branch algorithm be used? We found that every step we take is not necessarily the same, that is to say, every step we take is independent. Who knows if this step you take is the greatest value?

In other words, we have to consider the way of dynamic programming. Our next step is related to the result of the previous step. It is built on the basis of the previous step and fits our dynamic programming.

You can only follow the next row of adjacent digital paths. Each step is the same. This is the same point. The result of the next step is established on the basis of the result of the previous step. All conform to dynamic programming, so use dynamic programming

Code

public class 数字三角形 {
    
    
	public static void main(String[] args) {
    
    
		 Scanner scan = new Scanner(System.in);
	     //在此输入您的代码...
		 //1.二维数组来存储我们的三角形
		 //1.1我们的行数
		 int N = scan.nextInt();
		 int[][] arr = new int[N+1][N+1];
		 //1.2生成数字三角形
		 for (int i = 1; i <= N; i++) {
    
    
			for (int j = 1; j <= i; j++) {
    
    
				arr[i][j] = scan.nextInt();
			}
		 }
		 //2.计算我们的最大值
		 for (int i = 1; i <= N; i++) {
    
    
			for (int j = 1; j <= i; j++) {
    
    
				arr[i][j] = arr[i][j] + Math.max(arr[i-1][j], arr[i-1][j-1]);
			}
		}
		 //
		 if(N%2 != 0){
    
    
			 System.out.println(arr[N][N/2+1]);
		 }else{
    
    
	         System.out.println(Math.max(arr[N][N/2+1],arr[N][N/2]));
		 }
	     scan.close();
	}
}

At the beginning I didn’t know what the solution was, because I hadn’t seen it before, so I had to debug step by step to understand his thoughts. It’s easy to understand by just drawing a picture.

The first step :

arr [1]1 = arr[1] 1 + max[0][1 is meaningless, we have no results in the first round

Second step

i = 2 j = 1

arr[2][1] = arr[2][1] + Math.max(arr[1][1], arr[1][0]); ==>  10

i = 2 j = 2

arr[2][2] = arr[2][2] + Math.max(arr[1][2], arr[1][1]);
		  =  8        +       0         7   = 15

Insert picture description here

third step

i = 3 j = 1

arr[3][1] = arr[3][1] + Math.max(arr[2][1], arr[2][0]); ==>
    			8	+		(10,0)     =   18

i = 3 j = 2

arr[3][2] = arr[3][2] + Math.max(arr[2][2], arr[2][1]); ==>  
     			1    +   (15,10)      =  16

i = 3 j = 3

arr[3][3] = arr[3][3] + Math.max(arr[2][3], arr[2][2]); ==> 
    			0	+		(0,15)			 =    15

the fourth step

The same steps become

Insert picture description here

Finally becomes

Insert picture description here

We have another question, why is the answer to 27 when we know that 30 is the biggest

The main reason is that our problem requires that the difference between the number of times to go down left and the number of times to go down right should not exceed 1. How do we understand this?

Look at how 30 came to 7 3 8 7 5, of which 7 3 8 5 went down right 3 times, and 7 went down left only once, which does not meet the meaning of the question, then we will understand, as long as the difference cannot be more than 1, then It must be the value in the middle so it is 27

Guess you like

Origin blog.csdn.net/qq_22155255/article/details/114481570