It's not surprising that 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.
# Include <iostream> using namespace STD; # define n 102int array [N] [N], B [N] [N]; int getmax (int I, Int J) {int temp, Max; temp = max = 0; If (B [I] [J]! =-1) return B [I] [J]; else {If (array [I] [J]> array [I] [J + 1]) {temp = getmax (I, j + 1); If (temp> MAX) max = temp ;} if (array [I] [J]> array [I] [J-1]) {temp = getmax (I, J-1); If (temp> MAX) max = temp ;} if (array [I] [J]> array [I + 1] [J]) {temp = getmax (I + 1, J); If (temp> MAX) max = temp;} If (array [I] [J]> array [I-1] [J]) {temp = getmax (I-1, J); If (temp> MAX) max = temp;} B [I] [J] = MAX + 1; return B [I] [J] ;}} int main () {int I, J; for (I = 0; I <n; I ++) array [I] [0] = array [0] [I] = array [N-1] [I] = array [I] [N-1] = 10001; int R, C; cin> r> C; for (I = 1; I <= r; I ++) {for (j = 1; j <= C; j ++) {CIN> array [I] [J]; B [I] [J] =-1 ;}} int max = 0, temp; for (I = 1; I <= r; I ++) {for (j = 1; j <= C; j ++) {temp = getmax (I, j ); if (temp> MAX) max = temp ;}} cout <max <Endl; return 0 ;}