Should be very simple dynamic planning, to keep track of the state of each point at any time, so that the recursion when the direct use can be, do not need to look again, greatly reduce the time consumption.
The emphasis is on the state transition equation
Dp[x][y]=max (Dp[x-1][y], dp[x+1][y], dp[x][y-1], dp[x][y+1]) +1
Current maximum length is four directions maximum length plus 1
D-SkiingTime
limit:1000MS
Memory Limit:65536KB
64bit IO Format:%i64d & %i64u Submit Status
Description
It's not surprising that Michael likes to ski, because skiing is really exciting. But to get the speed, the slippery area must tilt downward, and when you slide to the bottom, you have to go up the slope again or wait for the lift to load you. Michael wants to know the longest bottom landslide in a region. The area is given by a two-dimensional array. Each digit of the array represents the height of the point. Here is an example
1 2 3
13 12 11) 10 9
One can slide from one point to the next four points adjacent to one another, when and only if the height decreases. In the example above, a sliding landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 longer. In fact, this is the longest one.
Input
The first line of input represents the number of rows in the range R and the number of columns C (1 <= r,c <= 100). The following are the r lines, each with a C integer representing a height of h,0<=h<=10000.
Output
The length of the longest region of the output.
Sample Input
5 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Sample Output
25
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <math.h>5#include <algorithm>6 using namespacestd;7 inthigh[102][102];8 intshigh[102][102]={0};9 intmax1=0, max2=0, C,r;Ten intdx[4]={1,-1,0,0},dy[4]={0,0,1,-1}; One intCheckintIintj) A { - intt,h; - if(Shigh[i][j]) the returnSHIGH[I][J];//you don't need to repeat the calculation when you are recursive - for(h=0;h<4; h++) - if(i+dx[h]>=0&&i+dx[h]<r&&j+dy[h]>=0&&j+dy[h]<c&&high[i+dx[h]][j+dy[h]]<high[i][j])//Determine if out of bounds and whether the conditions are met -{T=check (i+dx[h],j+dy[h]);if(T>=shigh[i][j]) shigh[i][j]=t+1;} T stores the length of the current direction, and if it is greater than or equal to the current position, add 1 and replace + returnshigh[i][j];//Returns the maximum length of this position - } + intorder () A { at inti,j; - for(i=0; i<r;i++) - for(j=0; j<c;j++) - check (i,j); - for(i=0; i<r;i++) - for(j=0; j<c;j++) in if(shigh[i][j]>max2) -Max2=Shigh[i][j]; to returnMax2; + } - intMain () the { * inti,j; $scanf"%d%d",&r,&c);Panax Notoginseng for(i=0; i<r;i++) - for(j=0; j<c;j++) thescanf"%d",&high[i][j]); +printf"%d\n", order () +1); A return 0; the}
Attach a time-out code because there is no real-time storage of the state with an array, resulting in timeouts, remember
1#include <iostream>2#include <stdio.h>3#include <string.h>4#include <math.h>5#include <algorithm>6 using namespacestd;7 inthigh[102][102];8 intmax1=0, Max2,c,r;9 intdx[4]={1,-1,0,0},dy[4]={0,0,1,-1};Ten intCheckintIintJintMark) One { A intt,h; - for(h=0;h<4; h++) - if(i+dx[h]>=0&&i+dx[h]<r&&j+dy[h]>=0&&j+dy[h]<c&&high[i+dx[h]][j+dy[h]]<High[i][j]) the{if((T=check (i+dx[h],j+dy[h],mark+1)) >MAX2) max2=t;} - returnMark; - } - intorder () + { - inti,j; + for(i=0; i<r;i++) A for(j=0; j<c;j++) at { -Max2=0; -Check (I,j,0); - if(max2>max1) -max1=Max2; - } in returnmax1+1; - } to intMain () + { - inti,j; thescanf"%d%d",&r,&c); * for(i=0; i<r;i++) $ for(j=0; j<c;j++)Panax Notoginsengscanf"%d",&high[i][j]); -printf"%d\n", order ()); the return 0; +}
Skiing (Dynamic planning)