POJ 1088 滑雪 (记忆化搜索)
2015-08-22 15:53
399 查看
DescriptionMichael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。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 9Sample Output
25
#include <iostream>#include <string.h>using namespace std;int map[101][101],R,C;int step[101][101];//step[i][j]代表当前坐标(i,j)所能达到的最大距离,不包含自己本身int der[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//上下左右四个方向bool ok(int x,int y){ if(x>=1&&x<=R&&y>=1&&y<=C)return true; else return false;}int DFS(int x,int y){ if(step[x][y]!=0)return step[x][y];//已经搜索过的就直接返回其值 for(int i=0;i<4;i++) { int x1=x+der[i][0]; int y1=y+der[i][1]; if(ok(x1,y1)==true&&map[x1][y1]<map[x][y]) { int temp=DFS(x1,y1)+1; if(step[x][y]<temp)//四个方向取最大值 step[x][y]=temp; } } return step[x][y];}int main(){ cin>>R>>C; for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) cin>>map[i][j]; memset(step,0,sizeof(step)); int ans=0; for(int i=1;i<=R;i++) for(int j=1;j<=C;j++) { step[i][j]=DFS(i,j); if(ans<step[i][j]) ans=step[i][j]; } cout<<ans+1<<endl;//本身算是序列中的第一个,所以要加1 return 0;}
相关文章推荐
- leetcode:Wildcard Matching
- 集训第六周 M题
- 阿里题目总结——阿里巴巴2015研发工程师B笔试卷
- Ubuntu12.04 安装adb
- 链表
- Search a 2D Matrix II
- SBJson的使用
- UIButton(继承自UIControl)
- UVA 10970 第一次比赛 D题 (后面才补的)
- redis数据存储系统原理
- s3c6410移植android-kernel-2.6.35内核不识别DM9000网卡
- idea 导入 cordova 生成项目报错 Unable to execute DX
- swing和java里嵌入浏览器
- oc中的文件
- 剑指offer面试题12-打印1到最大的n位数
- Ecshop、Discuz! 等开源产品的局限
- UITextField
- 湖南省第十届大学生计算机程序设计竞赛1511: 残缺的棋盘
- poj2280--Amphiphilic Carbon Molecules(扫描线+极角排序+转换坐标)
- TCP协议中的三次握手及四次握手图解