滑雪
Glory非常喜欢玩滑滑梯游戏,下面给出了一个n,m的滑道,其中的数字表示滑道的高度。Glory可以从一个点出发向下滑行,每次只能滑行到相邻的位置(上下左右)中高度严格低于当前高度的地方,不能重复划行已经滑行过的地方,但他希望在这个滑道上滑行尽量远的距离,也即是找一条最长的滑道。
Input
第一行输入两个数n,m代表滑梯范围行n和列m(1 <= n,m <= 100)。下面是n行,每行有m个整数,代表高度h,(0<=h<=20000)
Output
输出一个值,代表Glory能够在滑滑梯上面滑行的最长长度是多少
Sample Input
3 3
9 1 2
5 6 7
8 4 3
Sample Output
4
Sample Input
4 7
7 6 5 4 3 2 1
1 5 1 1 1 1 1
1 4 3 1 1 1 1
1 5 6 7 8 1 1
Sample Output
7
hint
样例1:7->6->4->3 长度为4
ac代码
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int d[4][2]={0,1,1,0,0,-1,-1,0};
int dp[101][101];
int ma[101][101];
int n,m;
int ans=0;
int dfs(int x,int y){
if(dp[x][y]!=-1) return dp[x][y];
int maxa=1;
int t;
for(int i=0;i<4;i++){
int xx=x+d[i][0];
int yy=y+d[i][1];
if(xx>=0&&xx<n&&yy>=0&&yy<m&&ma[xx][yy]>ma[x][y]){
t=dfs(xx,yy)+1;
maxa=max(maxa,t);
}
}
dp[x][y]=maxa;
return maxa;
}
int main(){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>ma[i][j];
}
}
memset(dp,-1,sizeof(dp));
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
dp[i][j]=dfs(i,j);
ans=max(ans,dp[i][j]);
}
}
cout<<ans<<endl;
return 0;
}