您的位置:首页 > 其它

POJ 1088 滑雪 记忆化搜索

2015-10-15 17:13 561 查看
滑雪

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 85082 Accepted: 31876
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

Source
SHTSC 2002

dp[i][j]表示长度,从i=1,j=1;开始搜索如果四周有大于a[i][j]例如a[i-1][j]那么递归求temp=max(a[i-1][j],dfs(i-1,j)
循环跟新每个点的高度最后循环求出dp[i][j]的最大值。
ACcode:

#pragma warning(disable:4786)//使命名长度不受限制
#pragma comment(linker, "/STACK:102400000,102400000")//手工开栈
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <stack>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define rds(x) scanf("%s",x)
#define rdc(x) scanf("%c",&x)
#define ll long long int
#define maxn 105
#define mod 1000000007
#define INF 0x3f3f3f3f //int 最大值
#define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;++i)
#define MT(x,i) memset(x,i,sizeof(x))
#define PI acos(-1.0)
#define E exp(1)
using namespace std;
int a[maxn][maxn],dp[maxn][maxn],n,m;
int dfs(int x,int y){
if(dp[x][y]!=0)
return dp[x][y];
//if(x<1||x>n||y<1||y>m)
// return 0;
if(x==0 || x==n+1 || y==0 || y==m+1)
return 0;
int cnt=0;
if(a[x-1][y]<a[x][y])
cnt=max(cnt,dfs(x-1,y));
if(a[x+1][y]<a[x][y])
cnt=max(cnt,dfs(x+1,y));
if(a[x][y-1]<a[x][y])
cnt=max(cnt,dfs(x,y-1));
if(a[x][y+1]<a[x][y])
cnt=max(cnt,dfs(x,y+1));
dp[x][y]=cnt+1;
return cnt+1;
}
int main(){
while(rd2(n,m )!=EOF){
MT(dp,0);
FOR(i,1,n)
FOR(j,1,m)
rd(a[i][j]);
FOR(i,1,n)
FOR(j,1,m)
dfs(i,j);
int maxxx=-1;
FOR(i,1,n)
FOR(j,1,m)
maxxx=max(maxxx,dp[i][j]);
printf("%d\n",maxxx);
}
return 0;
}
/*
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*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: