Description

Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5

16 17 18 19 6

15 24 25 20 7

14 23 22 21 8

13 12 11 10 9

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。
Input

输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
Output

输出最长区域的长度。
Sample Input

5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
Sample Output

25

#include<algorithm>
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
#define mst(a,b)  memset((a),(b),sizeof(a));
const int max1=105;
long long int dp[max1][max1];
//int sum[max1];
long long int ob[max1][max1];
int dx[5]={0,-1,0,1,0},dy[5]={0,0,-1,0,1};//dx与dy一组,构成上下左右四个方向
long long int r,//长c,//宽t,//做中间变量,用来寻找最大的ansans;//预设答案变量
int search(int x,int y);//声明search函数
int main()
{cin>>r>>c;//输入长宽ans=0;//无合适条件下设ans为最小值0for(int i=1;i<=r;i++)for(int j=1;j<=c;j++)cin>>ob[i][j];//输入矩阵for(int i=1;i<=r;i++)for(int j=1;j<=c;j++){//遍历矩阵中每一个点t=search(i,j);//调用searc函数dp[i][j]=t;if(t>ans) ans=t;}cout<<ans<<endl;
}
int search(int x,int y)
{int w,tmp,nx,ny;if(dp[x][y]>0){ //如果dp大于0代表dp被调用过可以直接使用return(dp[x][y]);//返回dp[i][j]的值}w=1;for(int i=1;i<=4;i++){//遍历四个方向nx=x+dx[i];ny=y+dy[i];if((nx>=1)&&(nx<=r)&&(ny>=1)&&(ny<=c)&&(ob[x][y]<ob[nx][ny])){//边界tmp=search(nx,ny)+1;//递归求当前点能够到达的最大值if(tmp>w) w=tmp;}}dp[x][y]=w;return w;
}
/*
5 5
1  2  3  4  5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
*/

FatMouse and Cheese

Problem Description

FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 <= p < n and 0 <= q < n. At each grid location Fatmouse has hid between 0 and 100 blocks of cheese in a hole. Now he’s going to enjoy his favorite food.

FatMouse begins by standing at location (0,0). He eats up the cheese where he stands and then runs either horizontally or vertically to another location. The problem is that there is a super Cat named Top Killer sitting near his hole, so each time he can run at most k locations to get into the hole before being caught by Top Killer. What is worse – after eating up the cheese at one location, FatMouse gets fatter. So in order to gain enough energy for his next run, he has to run to a location which have more blocks of cheese than those that were at the current hole.

Given n, k, and the number of blocks of cheese at each grid location, compute the maximum amount of cheese FatMouse can eat before being unable to move.

Input

There are several test cases. Each test case consists of

a line containing two integers between 1 and 100: n and k
n lines, each with n numbers: the first line contains the number of blocks of cheese at locations (0,0) (0,1) … (0,n-1); the next line contains the number of blocks of cheese at locations (1,0), (1,1), … (1,n-1), and so on.
The input ends with a pair of -1’s.

Output

For each test case output in a line the single integer giving the number of blocks of cheese collected.

Sample Input

3 1 1 2 5 10 11 6 12 12 7 -1 -1

Sample Output

37

#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int ob[10050][105];
int dp[10050][105];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
int x,y,ans,temp,k;
int search(int q,int w);
int main()
{while(cin>>x>>k){memset(ob,0,sizeof(ob));memset(dp,0,sizeof(dp));if(x==-1||y==-1) break;y=x;ans=0;for(int i=1;i<=y;i++)for(int t=1;t<=x;t++) cin>>ob[i][t];cout<<search(1,1)<<endl;}
}
int search(int q,int w)
{int nx,ny,t=0;if(dp[q][w]>0) return dp[q][w];for(int j=1;j<=k;j++)for(int i=0;i<4;i++){nx=q+dx[i]*j;ny=w+dy[i]*j;if((nx>=1)&&(nx<=x)&&(ny>=1)&&(ny<=y)&&(ob[q][w]<ob[nx][ny]))t=max(t,search(nx,ny));}return dp[q][w]=t+ob[q][w];
}

POJ1088 滑雪题解+HDU 1078(记忆化搜索DP)相关推荐

  1. hdu 1078 记忆化搜索

    题意:给出n*n的格子,每个各自里面有些食物,问一只老鼠每次走最多k步所能吃到的最多的食物 虽然是做过的老题了,但是1A的感觉好爽,对搜索认识更深了 1 #include<cstdio> ...

  2. hdu 1514 记忆化搜索

    题意是给4堆(堆的高度小于等于40)有颜色(颜色的种类小于等于20)的物品,你有一个篮子最多能装5件物品,每次从这4堆物品里面 任取一件物品放进篮子里,但是取每堆物品时,必须先取上面的物品,才能取下面 ...

  3. [蓝桥杯][算法提高VIP]夺宝奇兵(记忆化搜索||DP)

    题目描述 在一座山上,有很多很多珠宝,它们散落在山底通往山顶的每条道路上,不同道路上的珠宝的数目也各不相同.下图为一张藏宝地图: 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 " ...

  4. [P1434 [SHOI2002]滑雪](DFS,记忆化搜索)

    P1434 [SHOI2002]滑雪 题目描述 Michael喜欢滑雪.这并不奇怪,因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你 ...

  5. hdu 4722(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4722 思路:简单的记忆化搜索,留意一下A==0时的情况就可以了. 1 #include<iost ...

  6. hdu 1142 记忆化搜索

    题目是这样的,貌似一开始我这个英语搓的人还理解错了...orz http://acm.hdu.edu.cn/showproblem.php?pid=1142 就是最短路,只不过用dijkstra是从终 ...

  7. 【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

    题干: 小乐乐一天天就知道玩,这一天又想玩象棋. 我们都知道马走日. 现在给定一个棋盘,大小是n*m,把棋盘放在第一象限,棋盘的左下角是(0,0),右上角是(n - 1, m - 1); 小乐乐想知道 ...

  8. UVA10739 String to Palindrome【记忆化搜索+DP】

    In this problem you are asked to convert a string into a palindrome with minimum number of operation ...

  9. 【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

    题干: FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimens ...

最新文章

  1. rtsp有没有好使_轻量级RTSP服务和内置RTSP网关有什么不同?
  2. Asp.net 2.0 中将网站首页生成静态页的一个比较好的方法
  3. C# Socket编程(5)使用TCP Socket
  4. 3. SQL -- 存储过程
  5. 判断两个字符串是否为旋转词
  6. 研制一个生产计划编制的软件
  7. Linux Kbuild文档 1
  8. C++调用MATLAB函数
  9. 【elasticsearch】 Elasticsearch集群规模和容量规划的底层逻辑
  10. C++ Qt学习笔记(2)简易计算器设计(为计算器添加菜单功能)
  11. java和scala_什么是Scala及它与Java的区别
  12. Unity3D基础5:摄像机与Game视图
  13. 【Rollo的Python之路】比较运算符
  14. 【知识图谱系列】知识图谱多跳推理之强化学习
  15. GPS定位基本原理解析
  16. 利用爬虫编译翻译器 (包含防御反爬虫)
  17. 精益生产理论学习总结(一)
  18. 树莓派+新型混合无人机
  19. 最左前缀 mysql优化器_mysql查询优化之索引类型、最左前缀
  20. DINGWAVE发布口袋式软件无线电解决方案

热门文章

  1. 功率谱 幅值谱_疲劳损伤谱(FDS)的基本原理
  2. 深入理解session过期机制
  3. JS(JavaScript)的初了解3(更新中···)
  4. (笔记)java环境变量设置
  5. Renascence架构原理——最优化算法
  6. could not perform addBatch
  7. iOS 5将加入全新的通知信息和桌面Widgets
  8. 测试mysql连接服务器_实现服务器与数据库的连接
  9. Redis 集群搭建和简单使用
  10. php工程模式,factory - PHP工程模式如何传入参数