[洛谷] P1508 吃吃吃

[洛谷] P1508 吃吃吃_第1张图片

简单DP

注意最后取值即可

//#pragma GCC optimize(2)
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define int long long
const int MAXN = 2e3 + 10;

int arr[MAXN][MAXN];
int dp[MAXN][MAXN];

int n, m;

signed main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0);     cout.tie(0);
    //freopen("D://test.in", "r", stdin);
    //freopen("D://test.out", "w", stdout);
    
    cin>>n>>m;

    int mid = m / 2 + 1;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            cin>>arr[i][j];

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            dp[i][j] = max(max(dp[i-1][j], dp[i-1][j-1]), dp[i-1][j+1]) + arr[i][j];

    int ans = -0x3f3f3f3f;

    
    ans = max(max(dp[n][mid-1],dp[n][mid]),dp[n][mid+1]);
    
    cout<

 

你可能感兴趣的:(搜索,[Dynamic)