C递归记忆化搜索写的,求判断第二个哪错了

P1216 [USACO1.5] [IOI1994]数字三角形 Number Triangles

24222400583Xyy @ 2023-11-15 21:21:10

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max(a,b) (a)>(b)?(a):(b)
int dp[1005][1005];
int a[1005][1005];
int Solve(int i, int j,int r)
{
    if(dp[i][j] >= 0) return dp[i][j];
    return dp[i][j] = a[i][j] + (i == r ? 0 : max(Solve(i+1,j, r), Solve(i+1,j+1, r)) );
}
int main()
{
    int r;
    scanf("%d", &r);
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= i; j++)
            scanf("%d", a[i] + j);
    memset(dp, -1, sizeof(dp));
    int MAX = Solve(1, 1, r);
    printf("%d", MAX);
}
我稍微改了下就不行了
```c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define Max(a,b) (a)>(b)?(a):(b)
int a[1005][1005], dp[1005][1005];
int Solve(int i, int j, int r)
{

    if(dp[i][j] >= 0) return dp[i][j];
    if(i == r)return dp[i][j] = a[i][j];
    return dp[i][j] = a[i][j] + Max(Solve(i+1, j, r), Solve(i+1, j+1, r));
}
int main()
{
    int r;
    scanf("%d", &r);
    for(int i = 1; i <= r; i++)
        for(int j = 1; j <= i; j++)
            scanf("%d", a[i]+j);
    memset(dp, -1, sizeof(dp));
    int sum = Solve(1, 1, r);
    printf("%d", sum);
}

by 24222400583Xyy @ 2023-11-15 21:23:13

底下那个案例输出为4,头痛


|