收集宝藏

家里的网出问题了 只能吧博客写好一起在别人家发表
Description
有一个n*n的矩阵,矩阵每个格子中都有一些宝藏,从左上角(1, 1)出发,每次只能向下或者向右移动一格,已知每个格子中宝藏的价值,求走到右下角(n, n)时能收集到的宝藏的总最大价值。
Input
第一行为一个整数n(1 <= n <= 1000),表示矩阵的行、列数。
Output
一个整数,表示能收集到的宝藏的最大总价值。
Sample Input
4
1 2 3 10
3 4 1 1
5 2 1 1
1 3 1 1
Sample Output
19
Source

Unkown*/

#include<iostream>
using namespace std;
int main()
{
    int N;
    int map[1005][1005],mmax[1005][1005];
    cin>>N;
    for(int a=0;a<N;a++){
        for(int b=0;b<N;b++){
            cin>>map[a][b];
        }
    }
    for(int h=0;h<N;h++){
        for(int l=0;l<N;l++){
            if(h==0&&l==0){
                mmax[h][l]=map[h][l];
            }else if(h==0){
                mmax[h][l]=mmax[h][l-1]+map[h][l];
            }else if(l==0){
                mmax[h][l]=mmax[h-1][l]+map[h][l];
            }else{
                mmax[h][l]=max(mmax[h-1][l],mmax[h][l-1])+map[h][l];
            }
        }
    }
    cout<<mmax[N-1][N-1]<<endl;
    return 0;
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值