牛客假日团队赛5 K 金币馅饼 (DP 基础题)
链接:https://ac.nowcoder.com/acm/contest/984/K
来源:牛客网
金币馅饼
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
最近,奶牛们热衷于把金币包在面粉里,然后把它们烤成馅饼。第i块馅饼中含有Ni(1<=Ni<=25)块金币,并且,这个数字被醒目地标记在馅饼表面。
奶牛们把所有烤好的馅饼在草地上排成了一个R行(1<=R<=100)C列(1<=C<=100)的矩阵。你现在站在坐标为(1,1)的馅饼边上,当然,你可以拿到那块馅饼里的所有金币。你必须从现在的位置,走到草地的另一边,在坐标为(R,C)的馅饼旁边停止走动。每做一次移动,你必须走到下一列的某块馅饼旁边,并且,行数的变动不能超过1(也就是说,如果现在你站在坐标为(r,c)的馅饼边上,下一步你可以走到坐标为(r-1,c+1),(r,c+1),或者(r+1,c+1)的馅饼旁边)。当你从一块馅饼边经过,你就可以拿走馅饼里所有的金币。当然啦,你一定不会愿意因半路离开草地而失去唾手可得的金币,但,最终你一定得停在坐标为(R,C)的馅饼旁边。
现在,你拿到了一张标记着馅饼矩阵中,每一块馅饼含金币数量的表格。那么,按照规则,你最多可以拿到多少金币呢?
比方说,奶牛们把馅饼排成如下的矩阵,矩阵中的数字表示该位置的馅饼中含金币的数量:
起点-> 6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8 <-终点
以下是一条合法的路线:
起点-> 6 5 3 7 9 2 7
2 4 3 5 6 8 6
\ /
4 9 9-9 1 5-8 <-终点
按上述的路线进行走动,一共可以获得6+4+9+9+6+5+8=47个金币。按照规则,在这个矩阵中最多可以得到50个金币,路线如下图所示:
起点-> 6 5 3 7 9 2 7
2 4 3 5 6-8 6
\ /
4 9 9-9 1 5 8 <-终点
(请复制到记事本中用等宽字体查看)
输入描述:
第1行: 两个用空格隔开的整数,R和C
第2..R+1行: 每行包含C个用空格隔开的正整数,依次表示一行中从左往右各个馅饼里金币的数量
输出描述:
第1行: 输出一个正整数,表示你所能收集到的最大金币数目
示例1
输入
复制
3 7
6 5 3 7 9 2 7
2 4 3 5 6 8 6
4 9 9 9 1 5 8
输出
复制
50
思路:
直接二维DP 嘛,每一个位置可能由最多3个位置转移过来,还需要注意一下有些位置是没法访问到的,比如第一列,只有1,1这个位置可以访问,其他均无法到达,转移的时候需要判断一下。
细节见代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define rt return
#define dll(x) scanf("%I64d",&x)
#define xll(x) printf("%I64d\n",x)
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int dp[105][105];
int a[105][105];
int n,m;
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n>>m;
repd(i,1,n)
{
repd(j,1,m)
{
cin>>a[i][j];
}
}
memset(dp,-1,sizeof(dp));
dp[1][1]=a[1][1];
repd(j,2,m)
{
repd(i,1,n)
{
if(dp[i][j-1]!=-1)
dp[i][j]=max(dp[i][j],dp[i][j-1]+a[i][j]);
if(dp[i+1][j-1]!=-1)
dp[i][j]=max(dp[i][j],dp[i+1][j-1]+a[i][j]);
if(dp[i-1][j-1]!=-1)
dp[i][j]=max(dp[i][j],dp[i-1][j-1]+a[i][j]);
}
}
dp[n][m]=max(dp[n][m],0);
cout<<dp[n][m]<<endl;
return 0;
}
inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}
牛客假日团队赛5 K 金币馅饼 (DP 基础题)的更多相关文章
- 牛客假日团队赛10 L 乘积最大 (dp,大数)
链接:https://ac.nowcoder.com/acm/contest/1072/L?&headNav=acm&headNav=acm 来源:牛客网 乘积最大 时间限制:C/C+ ...
- P5200 [USACO19JAN]Sleepy Cow Sorting 牛客假日团队赛6 D 迷路的牛 (贪心)
链接:https://ac.nowcoder.com/acm/contest/993/E 来源:牛客网 对牛排序 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛5J 护城河 bzoj 1670: [Usaco2006 Oct]Building the Moat护城河的挖掘 (凸包的周长)
链接:https://ac.nowcoder.com/acm/contest/984/J 来源:牛客网 护城河 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 牛客假日团队赛5 L Catch That Cow HDU 2717 (BFS)
链接:https://ac.nowcoder.com/acm/contest/984/L 来源:牛客网 Catch That Cow 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 3 ...
- 牛客假日团队赛6 D 迷路的牛 (思维)
链接:https://ac.nowcoder.com/acm/contest/993/D 来源:牛客网 迷路的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)
链接:https://ac.nowcoder.com/acm/contest/984/F 来源:牛客网 随机数 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
- 洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)
链接:https://ac.nowcoder.com/acm/contest/984/A 来源:牛客网 题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,00 ...
- 「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)
链接:https://ac.nowcoder.com/acm/contest/984/D 来源:牛客网 饥饿的牛 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- 牛客假日团队赛2 C 修围栏 ( 哈夫曼树,贪心)
链接:https://ac.nowcoder.com/acm/contest/924/C 来源:牛客网 修围栏 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言6 ...
随机推荐
- MSTest/NUnit 单元测试 代码覆盖率试用 OpenCover 和ReportGenerator
VS自带是单元测试代码覆盖率(VS自带这个是最佳选择)需要企业版才有.很蛋疼...... 1.下载安装OpenCover 和ReportGenerator. 关于这2个是干啥的百度下.简单说就是可以分 ...
- LoadRunner 技巧之 添加事务
事务(Transaction)用于模拟用户的一个相对完整的.有意义的业务操作过程,例如登录.查询.交易.转账,这些都可以作为事务,而一般不会把每次HTTP请求作为一个事务. 拿笔者所测试的邮箱系统为例 ...
- mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题
<!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...
- 在线运行.NET代码
https://dotnetfiddle.net/ https://try.dot.net/ C# 发送Http协议 模拟 Post Get请求 1.参数 paramsValue的格式 要和 Requ ...
- Python学习之进程
8.2 进程 8.2.1 进程的创建 开启多进程scoketserver:server.client 进程的开启:python中的多线程,一定是有一个主进程,由主进程创建几个子进程, Linux与Wi ...
- Vue-cli 鼠标监听事件之滚动条
<template> <div class="scroll"> <div class="scroll-div-outer&quo ...
- mysql——多表——内连接查询
内连接查询:可以查询两个或者两个以上的表,当两个表中存在表示相同意义的字段时,可以通过该字段来连接这两个表: 当该字段的值相等时,就查询出该记录. 前期准备两个表: ), d_id ), name ) ...
- 利用Kruskal算法求最小生成树解决聪明的猴子问题 -- 数据结构
题目:聪明的猴子 链接:https://ac.nowcoder.com/acm/problem/19964 在一个热带雨林中生存着一群猴子,它们以树上的果子为生.昨天下了一场大雨,现在雨过天晴,但整个 ...
- 插入数据库失败([Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version)
报错信息如下: , ) 原因,read是数据库的关键字, 牢记,如果一个词是数据库的关键字,那么在写数据库语句的时候,这个词一定是蓝色的(关键字颜色)!!
- UDP协议&socketserver模块
UDP协议&socketserver模块 一.UDP协议 1.1 UDP实现简单通信 服务器 ------------------------------------------------- ...