Search gold(dp)
Search gold
Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others)
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)(1,1) and initially you have 0 gold.In the nn*mm 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)(x+1,y),(x,y+1),(x+1,y+2)and(x+2,y+1)(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)(1,1)is not a trap;
Input
first come 22 integers, n,mn,m(1≤n≤10001≤n≤1000,1≤m≤10001≤m≤1000)
Then follows nn lines with mm numbers aijaij
(−100<=aij<=100)(−100<=aij<=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 and output
| Sample Input | Sample Output |
|---|---|
3 3 |
5 |
3 3 |
1 |
题解:挖金矿,如果map当前值是负,表示花费一定金币,如果为负,这个人就死了;问从1,1出发,最多可以得到多少金币;、
就是个dp题,我却各种dp预处理无限wa,其实就可以顺着思路,dp初始化为-1;dp[1][1]=map[1][1],如果当前大于等于0,就往下走,总共有四种姿势;
找最大的就好;
代码:
extern "C++"{
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1.0);
typedef long long LL;
typedef unsigned u;
typedef unsigned long long uLL;
void SI(int &x){scanf("%d",&x);}
void SI(LL &x){scanf("%lld",&x);}
void SI(u &x){scanf("%u",&x);}
void SI(uLL &x){scanf("%llu",&x);}
void SI(double &x){scanf("%lf",&x);}
void SI(char *x){scanf("%s",&x);}
void PI(int &x){printf("%d",x);}
void PI(LL &x){printf("%lld",x);}
void PI(u &x){printf("%u",x);}
void PI(uLL &x){printf("%llu",x);}
void PI(double &x){printf("%lf",x);}
void PI(char *x){printf("%s",x);}
#define mem(x,y) memset(x,y,sizeof(x))
#define NL puts("");
}
const int MAXN = 1010;
int n,m;
int a[MAXN][MAXN];
int dp[MAXN][MAXN];
int ans;
int disx[4] = {1,0,1,2};
int disy[4] = {0,1,2,1};
int main(){
while(~scanf("%d%d",&n,&m)){
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
scanf("%d",&a[i][j]);
}
}
memset(dp,-1,sizeof(dp));
dp[1][1] = a[1][1];
if(a[1][1] < 0){
puts("0");
continue;
}
ans = a[1][1];
for(int i = 1;i <= n;i++){
for(int j = 1;j <= m;j++){
if(dp[i][j] >= 0){
for(int k = 0;k < 4;k++){
int nx = i + disx[k];
int ny = j + disy[k];
dp[nx][ny] = max(dp[nx][ny],a[nx][ny] + dp[i][j]);
ans = max(ans,dp[nx][ny]);
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
Search gold(dp)的更多相关文章
- Unique Binary Search Trees(dp)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode Unique Binary Search Trees (DP)
题意: 一棵BST有n个节点,每个节点的key刚好为1-n.问此树有多少种不同形态? 思路: 提示是动态规划. 考虑一颗有n个节点的BST和有n-1个节点的BST.从n-1到n只是增加了一个点n,那么 ...
- [SAP ABAP开发技术总结]搜索帮助Search Help (F4)
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- LightOJ 1033 Generating Palindromes(dp)
LightOJ 1033 Generating Palindromes(dp) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid= ...
- lightOJ 1047 Neighbor House (DP)
lightOJ 1047 Neighbor House (DP) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730# ...
- UVA11125 - Arrange Some Marbles(dp)
UVA11125 - Arrange Some Marbles(dp) option=com_onlinejudge&Itemid=8&category=24&page=sho ...
- 【POJ 3071】 Football(DP)
[POJ 3071] Football(DP) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4350 Accepted ...
- 由Leetcode详解算法 之 动态规划(DP)
因为最近一段时间接触了一些Leetcode上的题目,发现许多题目的解题思路相似,从中其实可以了解某类算法的一些应用场景. 这个随笔系列就是我尝试的分析总结,希望也能给大家一些启发. 动态规划的基本概念 ...
- 初探动态规划(DP)
学习qzz的命名,来写一篇关于动态规划(dp)的入门博客. 动态规划应该算是一个入门oier的坑,动态规划的抽象即神奇之处,让很多萌新 萌比. 写这篇博客的目标,就是想要用一些容易理解的方式,讲解入门 ...
随机推荐
- 基于MFC与第三方类CWebPage的百度地图API开发范例
在进行百度地图API开发之前你需要到http://developer.baidu.com/map申请密匙 密匙申请之后就可以进行百度地图API的开发了. 下面我们以在visual c++6.0里进行地 ...
- wxPython Major类
转载自:http://www.yiibai.com/wxpython/wxpython_major_classes.html 原始的 wxWidgets(用C++编写)是一个巨大的类库.GUI类从 ...
- NGUI研究之制作转圈的技能CD特效
昨天想做一个技能CD转圈的特效,花了大把的时间去用meshRender组件想通过三角形依据数学算法来绘制一个圆形的网格.通过动态绘制圆形网格的方法来实现技能CD特效.奶奶的昨天我研究了一晚上,最 ...
- Lua多重继承
http://blog.csdn.net/ssihc0/article/details/7742421 代码收藏了,以后用的到 --多重继承 local function search(k,plist ...
- [置顶] SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错
这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周.现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错 ...
- 使用 AngularJS 和 ReactJS 的经验
1. React 福音 当我们的团队开始寻找一个合适的前端框架的时候,我们考虑了许多选择,最后留下两个选项 —— Angular 和 React. Angular 是目前为止最成熟的方案:它拥有一个庞 ...
- LFS:kernel panic VFS: Unable to mount root fs
说明: 使用Vm虚拟机构建自己的LFS系统时,系统引导不成功,提示 kernel panic VFS: Unable to mount root fs 参考链接:http://www.52os.net ...
- win7系统64位安装oracle10g
win7系统64位安装oracle10g 下载地址: http://download.oracle.com/otn/nt/oracle10g/10204/10204_vista_w2k8_x64_pr ...
- IOS开发系列 --- 核心动画
原始地址:http://www.cnblogs.com/kenshincui/p/3972100.html 概览 在iOS中随处都可以看到绚丽的动画效果,实现这些动画的过程并不复杂,今天将带大家一窥i ...
- EntityFramework sum嵌套
一个查询中 用到了 sum , 可是返回结果的小数有很多位 , 都不准确了..类似js中的小数运算一样...不太熟悉C#,不知道这问题是因为double的关系 , 还是因为代码写的问题 , 通过 sq ...