Problem H. ICPC Quest (Syria ICPC)

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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMYBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值