Skiing time limit: 1000 msmemory limit: 65536 kbthis problem will be judged on UESTC. Original ID: 1309
64-bit integer Io format: % LLD Java class name: Main
Michael loves skiing because skiing is really exciting. But in order to get the speed, the slide area must be tilted down, and when you slide to the bottom, you have to go uphill again or wait for the elevator to carry you. Michael wants to know the longest landslide in a region. A region is given by a two-dimensional array. Each number in the array represents the vertex height. The following 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 certain point to one of the four adjacent points up and down, when and only when the height is reduced. In the preceding example, a slide 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 is an integer T, indicating the number of cases.
For each case:
The first line indicates the number of rows in the region R and the number of columns C (1 <= r, C <= 100 ). Below is the R row, each row has a C integer, representing the height H, 0 <= H <= 10000.
Output
The length of the maximum output area.
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
Sourceshtsc 2002: I used to worry about whether to search for it like this. In fact, this is from high to low, strictly from high to the end, and all will not be searched back!
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define INF 0x3f3f3f3f15 using namespace std;16 int mp[110][110],r,c,dp[110][110];17 int dfs(int x,int y,int h) {18 static int dir[4][2] = {0,-1,0,1,-1,0,1,0};19 if(x < 0 || x >= r || y < 0 || y >= c || mp[x][y] >= h) return 0;20 if(dp[x][y]) return dp[x][y];21 int theMax = 1;22 for(int i = 0; i < 4; i++){23 int tx = x+dir[i][0];24 int ty = y+dir[i][1];25 int temp = dfs(tx,ty,mp[x][y])+1;26 if(temp > theMax) theMax = temp;27 }28 return dp[x][y] = theMax;29 }30 int main() {31 int ans = 1;32 scanf("%d %d",&r,&c);33 for(int i = 0; i < r; i++){34 for(int j = 0; j < c; j++){35 scanf("%d",mp[i]+j);36 dp[i][j] = 0;37 }38 }39 for(int i = 0; i < r; i++){40 for(int j = 0; j < c; j++){41 ans = max(ans,dfs(i,j,INF));42 }43 }44 cout<<ans<<endl;45 return 0;46 }
View code