88tps哪里错了?
查看原帖
88tps哪里错了?
1012241
Dcchen楼主2023/12/14 21:28
#include <bits/stdc++.h>
using namespace std;

int n, w[1005][1005], dp[1005][1005];

void input() {
    cin >> n;
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= i; j++)
            cin >> w[i][j];
}

void work() {
    dp[1][1] =w[1][1]; // 初始条件

    for(int x = 2; x <= n; x++)
        for(int y = 1; y <= n; y++) {
            dp[x][y]=dp[x-1][y]+w[x][y];

            if(w[x-1][y-1]!=0) dp[x][y]=max(dp[x][y],dp[x-1][y-1]+w[x][y]);

            // ↑ 注意计算总和时,不要忘记有加上这格子的权重
            
        }

    int ans = -23333333;
    for(int i = 1; i <= n; i++)
        ans=max(ans,dp[n][i]); // 最后一行找最大值作为答案

    cout << ans << endl;
}

int main() {
	//freopen("P1216_9.in","r",stdin);
	//freopen("P1216_9.out","w",stdout);
    input();
    work();
   
    return 0;
}
2023/12/14 21:28
加载中...