zjut1633——好简单的动态规划
2016-03-26 20:17
225 查看
Problem A: Zero的樱花
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 75 Solved: 26
[Submit][Status][Web Board]
Description
十日樱花作意开,绕花岂惜日千回?昨宵风雨偏相厄,谁向人天诉此哀?忍见胡沙埋艳骨,空将清泪滴深怀。多情漫作他年忆,一寸春心早已灰。(黄季刚)
Zero不忍樱花就此落地,希望在樱花落地前接住落下的花瓣。Zero通过其强大的人脑计算能力,能够算出每一片樱花将在何时何地处于他能接住的高度,Zero必须刚好在那个时刻那个地点才能接住花瓣,如果迟到了会导致樱花飘落地面。
Zero十分灵活,每秒能够向东南西北任意一个方向移动1米,或者停留在原地,以便能够接到其他地方的樱花。Zero的初始位置可任选。
请你帮Zero算一下,他最多能接到多少片樱花花瓣?
Input
多组数据输入,第一行输入一个整数n(n<=20),表示有多少组数据。对于每组数据,先输入一个m(m<=100),表示花瓣数量。
接下来m行,每行代表一个花瓣,包含三个数据t(0<=t<=100)、x、y(0<x,y<=20),分别表示花瓣飘落到可接住高度的时间、横坐标、纵坐标。
Output
对于每组数据,输出一行,表示Zero最多能接到的花瓣数目。Sample Input
1 5 0 10 10 1 11 10 1 11 11 2 10 9 3 10 11
Sample Output
3很明显,t时刻xy位置时,后面可得到的花瓣数量是可以被已知的,动态规划即可。
#include<iostream> #include<algorithm> #include <stdio.h> #include<stdlib.h> #include<string> using namespace std; #define M 100010 int ans[105][22][22],flower[105][22][22]; int dp(int t,int x,int y) { int now_ans,maxx,xx,yy; if(ans[t][x][y]!=-1) return ans[t][x][y]; maxx=-1; xx=x-1; yy=y; if(xx>0) { now_ans=flower[t][x][y]+dp(t+1,xx,yy); if(now_ans>maxx) maxx=now_ans; } xx=x+1; yy=y; if(xx<=20) { now_ans=flower[t][x][y]+dp(t+1,xx,yy); if(now_ans>maxx) maxx=now_ans; } xx=x; yy=y+1; if(yy<=20) { now_ans=flower[t][x][y]+dp(t+1,xx,yy); if(now_ans>maxx) maxx=now_ans; } xx=x; yy=y-1; if(yy>0) { now_ans=flower[t][x][y]+dp(t+1,xx,yy); if(now_ans>maxx) maxx=now_ans; } xx=x; yy=y; now_ans=flower[t][x][y]+dp(t+1,xx,yy); if(now_ans>maxx) maxx=now_ans; ans[t][x][y]=maxx; return ans[t][x][y]; } int main() { int t,x,y,i,j,n,m,maxx; scanf("%d",&n); while(n--) { for(t=0;t<=100;t++) for(x=1;x<=20;x++) for(y=1;y<=20;y++) { flower[t][x][y]=0; ans[t][x][y]=-1; } scanf("%d",&m); while(m--) { scanf("%d%d%d",&t,&x,&y); flower[t][x][y]=1; } maxx=-1; for(i=1;i<=20;i++) for(j=1;j<=20;j++) { if(dp(0,i,j)>maxx) maxx=dp(0,i,j); } printf("%d\n",maxx); } }
相关文章推荐
- iOS Couldn't update the Keychain Item问题
- The Python Challenge
- 2016年3月23日作业
- CodeForces-626D-Jerry's Protest
- Tinyxml解析xml文件
- Tomcat热部署
- 剑指offer面试题10—二进制中1的个数
- IOS平台ASI框架 和 AFN框架 之Https通讯
- Unity3D热更新<一> 学习Lua
- 第四周上机实践项目 项目5--用递归方法求解(2)
- 深入线程:生产者与消费者问题
- gzoi2016酱油记
- URI数据存储初解
- 关于BufferedInputStream和FileInputStream的区别
- shell编程实践1
- hdoj--Let's go to play(模拟)
- 打印虚函数地址的方法
- java中多态的经典问题分析
- lua面向对象学习
- ElasticSearch中根据查询结果删除数据(delete by query)