题目地址:https://vjudge.net/problem/16378/origin
题目:
It's not surprising that Wanggann likes skiing, because skiing is really exciting. However, in order to gain speed, the sliding area must be tilted downwards, and when you slide to the bottom of the hill, you have to go uphill again or wait for the lift to load you.Wanggann wants to know the longest bottom landslide in one area. The area is given by a two-dimensional array. Each number in the array represents the height of the point. For example:
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
A slider can slide from one point up and down to one of the four adjacent points iff the height decreases. In the above example, a glideable landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.
Input
The first line entered indicates the number of rows R and the number of columns C (1 <= R, C <= 100). The following is an R row with C integers for each row, representing the height h, 0 <= h <= 10000.
Output
The length of the longest area.
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
思路:记忆化搜索,也就是dfs+dp。
代码:
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int maze[101][101];
int m,n;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int dp[101][101];
int dfs(int x,int y)
{
for(int i=0;i<4;i++)
{
int nx=x+dx[i],ny=y+dy[i];
if(nx>=0&&nx<m&&ny>=0&&ny<n&&maze[nx][ny]>maze[x][y]){
if(dp[nx][ny])dp[x][y]=max(dp[x][y],1+dp[nx][ny]);
else dp[x][y]=max(dp[x][y],1+dfs(nx,ny));
}
}
return dp[x][y];
}
int main()
{
while(cin>>m>>n){
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&maze[i][j]);
}
}
memset(dp,0,sizeof(dp));
int mmax=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(dp[i][j]==0)dfs(i,j);
mmax=max(mmax,dp[i][j]);
}
}
cout<<mmax+1<<endl;
}
return 0;
}