萌新求助!请问我这样从最下边向上递推有什么问题啊qaq
查看原帖
萌新求助!请问我这样从最下边向上递推有什么问题啊qaq
322896
xixi_bang楼主2020/4/26 20:14
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int maze[205][205];
ll dp[205][205];
int main(){
    std::ios::sync_with_stdio(false);
    int m,n;
    cin>>m>>n;
    for(int i=0;i<m;i++)
        for(int j=0;j<n;j++)
            cin>>maze[i][j];
    
    dp[m-1][n/2]=maze[m-1][n/2];
    dp[m-1][n/2-1]=maze[m-1][n/2-1];
    dp[m-1][n/2+1]=maze[m-1][n/2+1];
    for(int i=m-2;i>=0;i--)
        for(int j=0;j<n;j++){
            ll a=0,b=0,c=0;
            if(j>=1) a=dp[i+1][j-1];
            b=dp[i+1][j];
            if(j<n-1)
                c=dp[i+1][j+1];
            dp[i][j]=max(a,max(b,c))+maze[i][j];
        }
    ll ans=0;
    for(int j=0;j<n;j++)
        ans=max(ans,dp[0][j]);
    cout<<ans<<endl;



}
2020/4/26 20:14
加载中...