组合数学 - BZOJ 3997 - TJOI2015

本文解析了TJOI2015中一道关于寻找最少步数使网格中所有数字变为0的问题。通过动态规划的方法给出了高效的求解算法,并提供了C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TJOI2015 

Problem's Link

 ----------------------------------------------------------------------------

Mean: 

N×M的网格,一开始在(1,1)每次可以向下和向右走,每经过一个有数字的点最多能将数字减1,最终走到(N,M).

问至少要走多少次才能将数字全部变为0 (N,M<=1000,ai,j<=106)

analyse:

结论题.

d(i,j)
d(i,j)=max(d(i1,j),d(i,j+1),d(i1,j+1))+a[i,j]
答案是d(n,1)

Time complexity: O(N)

 

view code

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-15-13.19
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);

typedef long long ll;
ll d[1005][1005];
int main()
{
   int T;
   scanf("%d", &T);
   while(T--)
   {
       int n, m;
       scanf("%d%d", &n, &m);
       for(int i=1; i<=n; ++i)
       {
           for(int j=1; j<=m; ++j)
           {
               scanf("%lld", &d[i][j]);
           }
       }
       for(int i=1; i<=n; ++i)
       {
           for(int j=m; j>=1; --j)
           {
               d[i][j]=max(d[i][j]+d[i-1][j+1], max(d[i][j+1], d[i-1][j]));
           }
       }
       printf("%lld\n", d[n][1]);
   }
   return 0;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值