POJ-1088 Ski, a typical dynamic planning problem, like nyoj-10skiing, but nyoj on the time limit is 3s, with search can be past, but on the POJ on the timeout ~ ~

Source: Internet
Author: User
Tags cmath

Ski
Time Limit: 1000MS Memory Limit: 65536k
http://poj.org/problem?id=1088         

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  4 516 17 18 19 615 24 25 20 714 23 22 21 813 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

Source

SHTSC 2002

I laughed when I saw the problem on POJ, and I did it in Nanyang OJ (Nyoj) . Skiing "The same, Nyoj on the difficulty of 5, at that time with the search made to be quite excited, because the data range is not very large, with the search should be able to, the answer is immediately out, but there is no doubt-timeout, here the time limit is 1s, can only change a way of thinking;

Dynamic return is a freshman winter camp, seniors sent a PPT inside contains most of the POJ idea code, to see it provides two ideas:

(1)

(2)

Ideas one and two are similar, it only gives the idea, but the code can be a bit difficult to achieve, we may use the structure of the height of the coordinates to store, sort by the height sort, and then from small to large traversal to judge the height of the surrounding points, and then use either of the two ideas can be found in any of the answers;

Let's look at the time-out search code:

#include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include < Algorithm> #include <cmath>using namespace std;int a[100][100],maxn;void dfs (int x,int y,int s,int h,int l) {    if (x

      AC code:

<span style= "color: #ff0000;" > #include <cstdio> #include <cstring> #include <cstdlib> #include <iostream> #include < Algorithm> #include <cmath>using namespace std;const int n=10000+10;struct node{int x,y,h;//coordinates and height} a[n];int cmp (node A,node b)//Sort by height; {return a.h<b.h;}    int D[110][110],dp[110][110];int Main () {int hang,lie,i,j,k=0;    scanf ("%d%d", &hang,&lie);            For (I=1, i<=hang; i++) for (j=1; j<=lie; J + +) {a[++k].x=i,a[k].y=j;            scanf ("%d", &d[i][j]);            Dp[i][j]=1;        A[K].H=D[I][J];    } sort (a+1,a+k+1,cmp);    int maxx=0;            for (I=1; i<=k; i++)//Here is the idea of one, everyone is my type; {if (D[a[i].x][a[i].y]>d[a[i].x+1][a[i].y]) determine the height relationship of the next four directions, then the recursive formula is as follows;        Dp[a[i].x][a[i].y]=max (dp[a[i].x][a[i].y],dp[a[i].x+1][a[i].y]+1);         if (D[a[i].x][a[i].y]>d[a[i].x-1][a[i].y]) Dp[a[i].x][a[i].y]=max (dp[a[i].x][a[i].y],dp[a[i].x-1][a[i].y]+1); if (d[a[i].x][a[i].y]>d[a[i].x][a[i].y+1]) Dp[a[i].x][a[i].y]=max (dp[a[i].x][a[i].y],dp[a[i].x][a[i].y+1]+1);         if (D[a[i].x][a[i].y]>d[a[i].x][a[i].y-1]) Dp[a[i].x][a[i].y]=max (dp[a[i].x][a[i].y],dp[a[i].x][a[i].y-1]+1);    Maxx=max (Dp[a[i].x][a[i].y],maxx);    } printf ("%d\n", Maxx); return 0;} </span>

POJ-1088 Ski, a typical dynamic planning problem, like nyoj-10skiing, but nyoj on the time limit is 3s, with search can be past, but on the POJ on the timeout ~ ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.