The second provincial match of the 11th Blue Bridge Cup python group-digital triangle

1. Problem 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.
[Input format]
The first input line contains an integer N (1 <N ≤ 100), which represents the number of triangle lines. The next N lines give the digital triangle.
The numbers on the digital triangle are all integers between 0 and 100.
[Output format]
Output an integer to indicate the answer.
[Sample input]
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
[Sample output]
27

2. Thinking analysis:

Analyzing the question, you can know that this question is similar to the previous regular digital triangle question, but this question has an additional restriction that the difference between the number of times to go to the bottom left and the bottom right cannot exceed 1. In fact, this problem is solved. The key points of the question are also here. After reading some online ideas and then drawing a few simple examples to try the paths that can be taken, we can finally find that the answer is related to the parity of the N we input. When the input N is When the number is odd, the final answer is dp[n-1][n // 2], when the number is even, it is max(dp[n-1][n // 2-1], dp[n-1] [n // 2]), which is the larger of the two middle values. The following N is the path that can be taken when even and odd. You can find that the final answer is in the dp array obtained from the original digital triangle The dp value in the middle of the last line. Therefore, the path that can be taken according to the constraints of the subject becomes limited. Sometimes it is necessary to draw more pictures according to the conditions of the problem to find where the breakthrough is.

3. The code is as follows:

if __name__ == '__main__':
    n = int(input())
    matrix = list()
    for i in range(n):
        # 使用map函数将输入的字符串中的数字转为int类型, 最终使用list方法将其转换为一个列表
        matrix.append(list(map(int, input().split())))
    dp = [[0] * n for i in range(n)]
    dp[0][0] = matrix[0][0]
    for i in range(1, n):
        for j in range(i + 1):
            # 第一列
            if j == 0:
                dp[i][j] = dp[i - 1][j] + matrix[i][j]
            # 最后一列
            elif j == i:
                dp[i][j] = dp[i - 1][j - 1] + matrix[i][j]
            else:
                dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1]) + matrix[i][j]
    # print(dp)
    # 最后判断n是奇数还是偶数来返回对应的值
    # 奇数肯定是最中间的那个值偶数肯定是中间两个较大值
    print(dp[n - 1][n // 2] if n % 2 == 1 else max(dp[n - 1][n // 2 - 1], dp[n - 1][n // 2]))

 

Guess you like

Origin blog.csdn.net/qq_39445165/article/details/114991927