题目链接:http://poj.org/problem?id=1088
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
题意很明确,数据范围很明显暴力不出来(1s),记忆搜索,建一个数组,存起来查到的值,遇到的时候直接用就行了;
数组建立的时候r和c写反了。。。找了两个多小时的错误。。。估计该看脑子和眼睛 了。。,其他的都不难,就是在dfs的基础上加上一个数组就行了。
ac:
#include<stdio.h>
#include<string.h>
#include<math.h>
//#include<map>
//#include<set>
#include<deque>
#include<queue>
#include<stack>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
#define da 0x3f3f3f3f
#define clean(a,b) memset(a,b,sizeof(a))// 水印
int map[110][110];
int l[110][110];
int fx[4]={0,0,1,-1},fy[4]={1,-1,0,0};
//bool biaoji[110];
int r,c;
int max(int a,int b)
{
return a>b?a:b;
}
int judge(int x,int y)
{
if(l[x][y]!=0)
return l[x][y];
int len=1;
for(int i=0;i<4;++i)
{
int nowx=x+fx[i];
int nowy=y+fy[i];
if(nowx>=0&&nowy>=0&&nowx<r&&nowy<c)//就在这的r和c写反了。。注意。。
{
if(map[nowx][nowy]<map[x][y])
len=max(judge(nowx,nowy)+1,len);//比较四个方向上的,找出最长的那个方向上的路径,刷新一下;
}
}
return l[x][y]=len;//最后找到的那个存进记忆数组中;
}
int main()
{
cin>>r>>c;
for(int i=0;i<r;++i)
{
for(int j=0;j<c;++j)
cin>>map[i][j];
}
clean(l,0);
int res=0;
for(int i=0;i<r;++i)
{
for(int j=0;j<c;++j)
{
//clean(biaoji,0);
res=max(res,judge(i,j));
}
}
// for(int i=0;i<r;++i)
// {
// for(int j=0;j<c;++j)
// cout<<l[i][j]<<" ";
// cout<<endl;
// }
cout<<res<<endl;
return 0;
}