【NYOJ】 skiing

skiing

Time Limit: 3000  ms | Memory Limit: 65535  KB
Difficulty: 5
describe
It's no surprise that Michael loves skiing, because skiing is really exciting. But to gain speed, the slippery area has to slope down, and when you get to the bottom of the slope, you have to walk uphill again or wait for the lift to pick you up. Michael wants to know the longest landslide in an area. The area is given by a two-dimensional array. Each number of the array represents the height of the point. Here is an 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 person can slide from a point up, down, left and right to one of the four adjacent points if and Only when the height is reduced. In the example above, a slideable landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.
enter
The first line indicates that there are several sets of test data, and the second line of input indicates the number of rows R and the number of columns C of the region (1 <= R, C <= 100). Below are R lines, each with C integers representing height h, 0<=h<=10000.
Followed by the next set of data;
output
Output the length of the longest region.
sample input
1
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

#include<iostream>
#include<cstring>
using namespace std;
const int MAX = 105;
int n;
int map[MAX][MAX];
int dp[MAX][MAX];
int wid,hig;

int max(int a,int b,int c,int d){
	int max1 = a>b? a:b;
	int max2 = c>d? c:d;
	return max1>max2? max1:max2;
}

int maxlen(int i,int j){
	if(dp[i][j]>0) return dp[i][j];
	int a=0,b=0,c=0,d=0;
	if(i-1>=1&&(map[i][j]>map[i-1][j])){
		a=maxlen(i-1,j);
	}if(i+1<=hig&&(map[i][j]>map[i+1][j])){
		b=maxlen(i+1,j);
	}if(j-1>=1&&(map[i][j]>map[i][j-1])){
		c=maxlen(i,j-1);
	}if(j+1<=wid&&(map[i][j]>map[i][j+1])){
		d=maxlen(i,j+1);
	}
	return max(a,b,c,d)+1;
	
}

int main(){	
	cin>>n;
	while(n--){
		memset(dp,0,sizeof(dp));
		memset(map,0,sizeof(map));
		cin>>hig>>wid;
		for(int i=1;i<=hig;i++)
			for(int j=1;j<=wid;++j)
				cin>>map[i][j];
				
		for(int i=1;i<=hig;++i)
			for(int j=1;j<=wid;++j){
				dp[i][j]=maxlen(i,j);
			}
		
		int maxx = -100000;
		for(int i=1;i<=hig;++i)
			for(int j=1;j<=wid;++j){
				if(maxx<dp[i][j])
					maxx=dp[i][j];
			}
		cout<<maxx<<endl;
	}
	return 0;
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326886774&siteId=291194637