Blue Bridge Cup previous real questions--digital triangle (Java)

Blue Bridge Cup previous real questions--digital triangle (Java)

topic description

insert image description here

The figure above shows a number 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, and your task is to find the largest sum.

Each step on the path can only go from one number to the next level and its closest left or right number. In addition, the number of times to go down to the left and the number of times to go down to the right cannot differ by more than 1.

enter description

The first line of input contains an integer N (1≤N≤100) representing the number of rows of the triangle.

The next N lines give the number triangle. The numbers on the number triangle are all integers between 0 and 100.

output description

Output an integer representing the answer

example

enter

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

output
27

Idea analysis

This question is a dynamic programming problem with constraints. Each grid starting from the second row is descended from the left and right grids of the previous row. However, since it is required that the difference between the total number of times of going down to the left and the number of times of going down to the right cannot exceed 1, each grid has two states, one is the number of times left to go to the right and the number of times left to go to the left Times, and use -1 to indicate that the current grid cannot be reached.
Starting from the vertex, regardless of whether the total number of times to go is even or odd, the number of times the vertex goes to the left and to the right is initialized to n/2 (n is the number of rows, and the actual number of times to go is n - 1). But you can only go right when the number of times you go to the left is over, so the difference between the number of times you go down to the left and the number of times you go down to the right will not exceed 1.

import java.util.Scanner;

public class Main {
    
    
	public static void main(String[] args) {
    
    
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int[][] leftNum = new int[n + 1][];// 对应当前格子向左边可以走的次数
		int[][] rightNum = new int[n + 1][];// 对应当前格子向右边可以走的次数
		leftNum[1] = new int[1];
		rightNum[1] = new int[1];
		leftNum[1][0] = n / 2;//最上面的第一个格子初始化为n/2
		rightNum[1][0] = n / 2;
		int[][] array = new int[n + 1][];
		for (int i = 1; i <= n; i++) {
    
    
			array[i] = new int[i];
			for (int j = 0; j < i; j++) {
    
    
				array[i][j] = scan.nextInt();
			}
		}
		for (int j = 2; j <= n; j++) {
    
    
			leftNum[j] = new int[j];
			rightNum[j] = new int[j];
			for (int i = 0; i < j; i++) {
    
    
				if (i == j - 1 || leftNum[j - 1][i] == 0) {
    
    // 只能上一行往右下走
					if (i != 0 && rightNum[j - 1][i - 1] > 0) {
    
    // 如果不是,则没有办法到达此格子
						array[j][i] += array[j - 1][i - 1];
						rightNum[j][i] = rightNum[j - 1][i - 1] - 1;
						leftNum[j][i] = leftNum[j - 1][i - 1];
					} else {
    
    
						rightNum[j][i] = -1;//用-1标记
						leftNum[j][i] = -1;
					}
				} else if (i == 0 || rightNum[j - 1][i - 1] == 0) {
    
    // 只能上一行往左下走
					if (i != j - 1 && leftNum[j - 1][i] > 0) {
    
    
						array[j][i] += array[j - 1][i];
						leftNum[j][i] = leftNum[j - 1][i] - 1;
						rightNum[j][i] = rightNum[j - 1][i];
					} else {
    
    
						rightNum[j][i] = -1;
						leftNum[j][i] = -1;
					}
				} else {
    
    //左右都可以走
					if (array[j - 1][i] > array[j - 1][i - 1]) {
    
    
						array[j][i] += array[j - 1][i];
						leftNum[j][i] = leftNum[j - 1][i] - 1;
						rightNum[j][i] = rightNum[j - 1][i];
					} else {
    
    
						array[j][i] += array[j - 1][i - 1];
						rightNum[j][i] = rightNum[j - 1][i - 1] - 1;
						leftNum[j][i] = leftNum[j - 1][i - 1];
					}
				}
			}
		}
		//找出最大值
		int max = 0;
		for (int i = 0; i < n; i++) {
    
    
			if (array[n][i] > max) {
    
    
				max = array[n][i];
			}
		}
		System.out.println(max);
		scan.close();
	}
}

Guess you like

Origin blog.csdn.net/m0_57614677/article/details/122838022