ACM DP Classic Topic Ski problem DP Memory search recursion

Source: Internet
Author: User
Skiing Problems

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 5
16 17 18) 19 6
15 24 25) 20 7
14 23 22) 21 8
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 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



Analysis: DP Entry questions


#include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <
algorithm> #include <map> #include <set> using namespace std;
2015.4.1//Ski problem longest descent subsequence memory search recursive int r,c;
int a[105][105];
int ma[105][105];
int w[4]={0,0,1,-1};

int h[4]={1,-1,0,0};
    int find (int i,int j) {int m=0;
    if (Ma[i][j]) {return ma[i][j];
        } for (int k=0;k<4;k++) {int p=i+w[k];
        int q=j+h[k]; if (A[i][j]>a[p][q]&&p>0&&p<=r&&q>0&&q<=c&&m<find (p,q
        ) {M=find (i+w[k],j+h[k]);

}} return ma[i][j]=m+1;
        } int main () {while (true) {cin>>r>>c;
        int ans=0;
        memset (ma,0,sizeof (MA));
            for (int i=1;i<=r;i++) {for (int j=1;j<=c;j++) {cin>>a[i][j];
  }} for (int i=1;i<=r;i++)      {for (int j=1;j<=c;j++) {find (I,J);
                }} for (int i=1;i<=r;i++) {for (int j=1;j<=c;j++) {
                if (Ans<ma[i][j]) {ans=ma[i][j];
    }}} cout<<ans<<endl;
} return 0;
 }


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.