Problem H. ICPC Quest (Syria ICPC)

版权声明:版权所有--转载的小伙伴请告知博主并注明来源哦~ https://blog.csdn.net/u011145745/article/details/82055039

Problem H. ICPC Quest

来源:ACM International Collegiate Programming Contest, Syrian Collegiate Programming Contest (2014) Syria, September, 23, 2014 CF链接Gym 100500H

Problem Description

Noura Boubou is a Syrian volunteer at ACM ACPC (Arab Collegiate Programming Contest) since 2011. She graduated from Tishreen University where she studied Information Technology. She is the head of the photography team responsible for photo-shooting the Arabs joining the world finals.

On the first day of the world finals last year, Dr Mohamed Fo’ad, the ACPC Regional Contest Director, asked Noura to play one of the ICPC Quests. ICPC Quests are a set of challenges that might require the contestants to move around the city searching for some monuments, solving puzzles, or getting a high score in a certain game. The rules of the quests states that the contestants will post the answer to the quest to Twitter using these hashtags #ICPC2014 #QuestN where N is the quest number. The contestant will post a photo or a short video (a Vine) of the challenge he accomplished. The scores of the challenges are accumulated and the one with the highest score will get an Android tablet.

Noura was so enthusiastic about the Quest number 14. Quest 14 was about a grid of n rows and m columns, and each cell of the grid contains an integer, and whenever a contestant steps on a cell he is going to add its value to his total accumulated sum. The task was to start from the top left cell located at (1,1) and to move only right or down in order to get to cell (n,m), and during this journey the contestant has to get the maximum sum possible. The player who can get the maximum possible score is announced as the winner of this quest.

You will be given the description of the grid, please help Noura determining the maximum possible score she can get.

Input

The first line will be the number of test cases T. Each test case starts with 2 integers n, m where n is the number of rows while m is the number of columns. They will be followed by n rows each containing m numbers, and the absolute value of the number in each cell will not exceed 100.

1 <= T <= 100

1 <= n,m <= 1000

-100 <= celli,j <= 100

Output

For each test case print a single line containing:

Case_x:_y

x is the case number starting from 1.

y is is the required answer.

Replace underscores with spaces.

Examples

quest.in

2

3 3

1 2 3

-1 -2 -3

1 1 1

2 3

1 1 2

2 1 5

Standard Output

Case 1: 4

Case 2: 9

Hint:题目要求是求出从左上角(1,1)到右下角(n,m),只能向右或向下,找到最大的路径和;

第一种代码:(从最后一排最后一列开始向前找)

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int a[1100][1100], b[1100][1100];
int main()
{
    int t, i, j, k = 1, n, m;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= m; j++)
            {
                scanf("%d", &a[i][j]);
            }
        }
        for(i = n; i >= 1; i--)
        {
            for(j = m; j >= 1; j--)
            {
                if(i == n && j == m)
                    b[i][j] = a[i][j];
                else if(i == n)
                    b[i][j] = a[i][j] + b[i][j + 1];
                else if(j == m)
                    b[i][j] = a[i][j] + b[i + 1][j];
                else
                {
                    if(b[i][j + 1] >= b[i + 1][j])
                        b[i][j] = a[i][j] + b[i][j + 1];
                    else if(b[i][j + 1] < b[i + 1][j])
                        b[i][j] = a[i][j] + b[i + 1][j];
                }
            }
        }
        printf("Case %d: %d\n", k++, b[1][1]);
    }
    return 0;
}

第二种代码:(从前往后找,边输入边查询)

//#include <bits/stdc++.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

int a[1100][1100], b[1100][1100];
int main()
{
    int t, i, j, k = 1, n, m;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d %d", &n, &m);
        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= m; j++)
            {
                scanf("%d", &a[i][j]);
                b[i][j] = INT_MIN; // 定义无穷小
                if(i == 1 && j == 1)
                    b[i][j] = a[i][j];
                if(i > 1)
                    b[i][j] = max(b[i - 1][j] + a[i][j], b[i][j]);
                if(j > 1)
                    b[i][j] = max(b[i][j - 1] + a[i][j], b[i][j]);
            }
        }
        printf("Case %d: %d\n", k++, b[n][m]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/u011145745/article/details/82055039