AcWing 1015 picking peanuts

Title description:

Hello Kitty wants to pick some peanuts and give it to her favorite Mickey Mouse.

She came to a rectangular peanut field with grid-like roads (pictured below), went in from the northwest corner and came out from the southeast corner.

There is a peanut seedling planted at the intersection of each road in the field, with several peanuts on it, and all the peanuts on it can be picked after one peanut seedling.

Hello Kitty can only go east or south, not west or north.

Ask Hello Kitty how many peanuts can be picked at most.

1.gif

Input format

The first line is an integer T, which represents how many sets of data there are in total.

Next is the T group data.

The first row of each group of data is two integers, representing the row number R and the column number C of peanut seedlings.

The next R rows of data for each group of data describe the situation of each row of peanut seedlings from north to south. Each row of data has C integers, describing the number M of peanuts on each peanut seedling in the row in order from west to east.

Output format

For each set of input data, output one line, the content is the maximum number of peanuts that Hello Kitty can pick.

data range

1≤T≤100,
1≤R,C≤100,
0≤M≤1000

Input sample:

2
2 2
1 1
3 4
2 3
2 3 4
1 6 5

Sample output:

8
16
#include <iostream>
#include <cstdio>
#include <queue>

using namespace std;
const int MAX = 109;

int T, R, C, M;
int a[MAX][MAX], dp[MAX][MAX];


int main()
{
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d%d", &R, &C);

        for(int i = 1; i <= R; i++)
            for(int j = 1; j <= C; j++)
                scanf("%d", &a[i][j]);

        for(int i = 0; i < C; i++)
            dp[0][i] = 0;

        for(int i = 0; i < R; i++)
            dp[i][0] = 0;


        for(int i = 1; i <= R; i++)
        {
            for(int j = 1; j <= C; j++)
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + a[i][j];
        }

        printf("%d\n", dp[R][C]);

    }
    return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44620183/article/details/113745204