UESTC--1271--Search gold(贪心)
Time Limit: 1000MS | Memory Limit: 65535KB | 64bit IO Format: %lld & %llu |
Description
Dreams of finding lost treasure almost came true recently. A new machine called 'The Revealer' has been invented and it has been used to detect gold which has been buried in the ground. The machine was used in a cave near the seashore where – it is said
– pirates used to hide gold. The pirates would often bury gold in the cave and then fail to collect it. Armed with the new machine, a search party went into the cave hoping to find buried treasure. The leader of the party was examining the soil near the entrance
to the cave when the machine showed that there was gold under the ground. Very excited, the party dug a hole two feel deep. They finally found a small gold coin which was almost worthless. The party then searched the whole cave thoroughly but did not find
anything except an empty tin trunk. In spite of this, many people are confident that 'The Revealer' may reveal something of value fairly soon.
So,now you are in the point$(1,1)$ and initially you have 0 gold.In the $n$*$m$ grid there are some traps and you will lose gold.If your gold is not enough you will be die.And there are some treasure and you will get gold.If you are in the point(x,y),you
can only walk to point $(x+1,y),(x,y+1),(x+1,y+2)$and$(x+2,y+1)$.Of course you can not walk out of the grid.Tell me how many gold you can get most in the trip.
It`s guarantee that$ (1,1)$is not a trap;
Input
first come $2$ integers, $n,m$($1≤n≤1000$,$1≤m≤1000$)
Then follows $n$ lines with $m$ numbers $ a_{ij} $
$(-100<=a_{ij}<=100) $
the number in the grid means the gold you will get or lose.
Output
print how many gold you can get most.
Sample Input
3 3
1 1 1
1 -5 1
1 1 1
3 3
1 -100 -100
-100 -100 -100
-100 -100 -100
Sample Output
5
1
Hint
Source
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dx[4] = {1,0,1,2};
int dy[4] = {0,1,2,1};
int a[1005][1005];
int dp[1005][1005];
int m,n;
int main()
{
memset(dp,-1,sizeof(dp));
scanf("%d %d",&n,&m);
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
scanf("%d",&a[i][j]);
dp[1][1] = a[1][1];
int ans = -1;
for(int i = 1;i <= n;i++)
for(int j = 1;j <= m;j++)
{
if(dp[i][j] < 0)
continue;
ans=max(dp[i][j],ans);
for(int k=0;k<4;k++)
{
int x = i + dx[k];
int y = j + dy[k];
if(x<=0||x>n||y<=0||y>m)
continue;
dp[x][y] = max(dp[i][j] + a[x][y],dp[x][y]);
}
}
printf("%d\n",ans);
return 0;
}
UESTC--1271--Search gold(贪心)的更多相关文章
- CDOJ 1271 Search gold
简单DP.dp[i][j]表示走到这格的最大金钱数. #include<cstdio> #include<cstring> #include<cmath> #inc ...
- Search gold(dp)
Search gold Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- 集束搜索beam search和贪心搜索greedy search
贪心搜索(greedy search) 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search) 集束搜索可以认为是维特比算法的贪心形式,在维特 ...
- 【动态规划】CDOJ1271 Search gold
方格取数. 但由于题意说金币数<0就死了,就不能继续转移. #include<cstdio> #include<algorithm> #include<cstrin ...
- beam search 和 greedy search
贪心搜索(greedy search): 贪心搜索最为简单,直接选择每个输出的最大概率,直到出现终结符或最大句子长度. 集束搜索(beam search): 集束搜索可以认为是维特比算法的贪心形式,在 ...
- 第七届ACM趣味程序设计竞赛第四场(正式赛) 题解
Final Pan's prime numbers 题目连接: http://acm.uestc.edu.cn/#/problem/show/1272 题意 给你n,要求你在[4,n]范围内找到一个最 ...
- Andrew Ng机器学习公开课笔记 -- Regularization and Model Selection
网易公开课,第10,11课 notes,http://cs229.stanford.edu/notes/cs229-notes5.pdf Model Selection 首先需要解决的问题是,模型 ...
- 《Sequence Models》课堂笔记
Lesson 5 Sequence Models 这篇文章其实是 Coursera 上吴恩达老师的深度学习专业课程的第五门课程的课程笔记. 参考了其他人的笔记继续归纳的. 符号定义 假如我们想要建立一 ...
- deeplearning.ai 序列模型 Week 3 Sequence models & Attention mechanism
1. 基础模型 A. Sequence to sequence model:机器翻译.语音识别.(1. Sutskever et. al., 2014. Sequence to sequence le ...
随机推荐
- Python3没有dict.has_key方法
最近开始学习Python,安装上最新的Python3.3.3照书敲了一个小程序结果报错 'dict' object has no attribute 'has_key' 上网查也找不到解决办法,后来发 ...
- 使用Android ADT最新开发工具后,新建项目出现appcompat v7 他是什么?
做Android开发的朋友最近会发现,更新ADT至22.6.0版本之后,创建新的安装项目,会出现appcompat_v7的内容.并且是创建一个新的内容就会出现.这到底是怎么回事呢?原来appcompa ...
- css中单位的使用
css中许多的属性都需要添加长度,而长度一般由数字和单位构成,如1px,1.5em,2vh:也可以省略单位,如line-height:1.5,表示行高为字体大小的1.5倍: 长度单位一般也分为相对长度 ...
- CSS简单入门
- Java攻城狮学习路线 - 一. 什么是CSS CSS指层叠样式表(Cascading Style Sheets),定义如何显示HTML元素 二. CSS语法 /* 选择器 { 声明: 声明:}* ...
- hdu4081 Qin Shi Huang's National Road System 次小生成树
先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...
- PHP入门及服务环境配置(Nginx+PHP)
PHP入门及服务环境配置(Nginx+PHP) PHP入门 PHP维基百科: PHP(全称:PHP:Hypertext Preprocessor,即"PHP:超文本预处理器")是一 ...
- Absolute Horizontal And Vertical Centering In CSS
Quick CSS Trick: How To Center an Object Exactly In The Center Centering in CSS: A Complete Guide Ab ...
- VC++抛出自定义编译期异常的指令
#error 即可, 抛出消息是 #pragma message 最新的还有static_assert有一些用 一下子忘了网上居然搜不到...尝试了 关键字vc++.vc.vs.msvc + 抛出编 ...
- associatedtype关联类型
associatedtype关联类型 定义一个协议时,有的时候声明一个或多个关联类型作为协议定义的一部分将会非常有用.关联类型为协议中的某个类型提供了一个占位名(或者说别名),其代表的实际类型在协 ...
- 采用requests库构建简单的网络爬虫
Date: 2019-06-09 Author: Sun 我们分析格言网 https://www.geyanw.com/, 通过requests网络库和bs4解析库进行爬取此网站内容. 项目操作步 ...