P1508 Likecloud - Eat, Eat, Eat

topic background

Ask the world, what is adolescence?

The answer: "Hyperthyroidism, hyperthyroidism, hyperthyroidism again; starve, starve, starve again!"

Topic description

Li Da buffalo, which is in a certain period, has been in a state of starvation recently due to its relatively developed digestive system. One day in class, when he was dizzy with hunger, a huge rectangular dining table of n*m (n and m<=200) suddenly appeared in front of him, and he was on one side of the large dining table. below the midpoint. The dining table is divided into n*m small squares, and in each square there is a large round plate, which is full of food that Li Da Buffalo is thinking about. Li Da Buffalo has scored all the food on the table according to the energy it can provide (some are negative, because eating will cause diarrhea), he decided to eat from his position to the other side of the table, but He has a habit of eating - only eat the food in front of him, left or right.

Since the big buffalo Li is too hungry to think about it, and he wants to get the most energy, he turns this question over to you.

The starting point of each set of data is below the middle of the last row!

Input and output format

Input format:

 

[Input data:]

The first line is m n. (n is an odd number), and Li Da Buffalo starts at the bottom of the middle of the last line

Next is an m*n numeric matrix.

There are m lines in total, each with n numbers. The numbers are separated by spaces. It represents the energy provided by the food on the plate on the grid.

The numbers are all integers.

 

Output format:

 

[Output Data:]

A number that is the maximum energy value you have found.

 

Input and output example

Input Example #1:  Copy
6 7
16 4 3 12 6 0 3
4 -5 6 7 0 0 2
6 0 -1 -2 3 6 8
5 3 4 0 0 -2 7
-1 7 4 0 7 -5 6
0 -1 3 4 12 4 2
Output Sample #1:  Copy
41

illustrate

Fast food! Fast food! Fast food!

 

#include<bits/stdc++.h>


using namespace std;
int m,n,a[205][205],k[205][205],ans;
bool vis[205][205]; 
int dfs(int x,int y){
    if(vis[x][y]) return k[x][y];
    for(int i=-1;i<=1;i++){
        if(y+i>=1&&y+i<=n&&x-1>0)
            k[x][y] =max(k[x][y],dfs(x- 1 ,y+i)+ a[x][y]);
    }vis[x][y] = true ;
    return k[x][y];
}
intmain ()
{
    ios::sync_with_stdio(0);
    cin>>n>>m;
    memset(a,-9999,sizeof(a));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            cin>>a[i][j];
    memset(k,-0x3f,sizeof(k));
    for(int i=1;i<=m;i++) k[1][i]=a[1][i],vis[1][i]=true;
    dfs(n+1,m/2+1); 
    cout<<max(max(k[n][m/2+2],k[n][m/2+1]),k[n][m/2]);
    return 0;
}
View Code

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897569&siteId=291194637