http://poj.org/problem?id=1088
Subject description:
Glory the slides love to play a game, following a given n, m chute, wherein the digital representation of the height of the chute. Glory be starting from a point down the coast, you can only slide to an adjacent location (up and down) in height strict lower than the height of the current place, can not be repeated rowing places have been sliding, but he hopes this slide track glide as far as possible the distance, that is, to find a longest slide.
Input
The first input line of the two numbers n, m and n rows representative of the range of slide columns m (1 <= n, m <= 100). The following are n rows, each row having m integers, representing the height h, (0 <= h <= 20000)
Output
Outputting a value representative of Glory coasting slide the slide number is above the maximum length
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
Example 1: 7-> 6-> 4-> 3 length 4
The idea for the solution to a problem:
I feel a little like seeking the number of issues connected area, as are walking on a map thing.
The purpose is to prevent time-out record.
Dp stored in the array is the most distant point of each can go.
The main process is the longest length of the recording can reach a certain point. Then extended from one point to the four directions, the time to reach a certain point, if this point has been traversed, then the return value directly on the line, otherwise it made to traverse.
Code:
1 #include <cmath> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <climits> 6 #include <map> 7 #include <set> 8 #include <queue> 9 #include <stack> 10 #include <vector> 11 #include <string> 12 #include <iostream> 13 #include <algorithm> 14 15 #define N 100010 16 17 using namespace std; 18 19 typedef long long int ll; 20 int n, m; 21 int rec[110][110]; 22 int grap[110][110]; 23 int dx[4]={0, 1, 0, -1}; int dy[4]={1, 0, -1, 0}; 24 25 bool torf(int i, intJ) { 26 is return (I> = 0 && I <n-&& J> = 0 && J <m); // determines whether bounds 27 } 28 // find the maximum distance of this point from 29 int find_max ( int I, int J) { 30 IF (REC [I] [J]> 0 ) // If this point has been traversed 31 is return REC [I] [J]; 32 int K, MaxL; 33 is int NX, NY; 34 is MaxL = 0 ; 35 // traversal four directions 36 for (K = 0; K < . 4 ; K ++ ) { 37 [ NX = I + DX [K]; 38 is NY = J + Dy [K]; 39 // there is the next point value necessary and sufficient conditions than the current point value 40 IF (Torf (NX , NY) && The grap [NX] [NY]> The grap [I] [J]) { 41 is MaxL = max (MaxL, find_max (NX, NY)); 42 is } 43 is } 44 is return REC [I] [J] = + MaxL . 1 ; // able to walk itself plus the maximum distance 45 } 46 is int main () 47 { 48 int I, J; 49 Scanf ("%d%d", &n, &m); 50 for(i=0; i<n; i++){ 51 for(j=0; j<m; j++){ 52 scanf("%d", &grap[i][j]); 53 rec[i][j]=0; 54 } 55 } 56 int ans=INT_MIN; 57 for(i=0; i<n; i++){ 58 for(j=0; j<m; j++){ 59 max = ANS (ANS, find_max (I, J)); // every search value is updated at 60 } 61 is } 62 is the printf ( " % D \ n- " , ANS); 63 is return 0 ; 64 }