[ACM] POJ 1088 ski (Memory search Dfs)

Source: Internet
Author: User

Ski
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 73409 Accepted: 27141

Description

It's not surprising that Michael likes to ski, because skiing is really exciting.

But in order to get the speed, the sliding area must tilt downward and when you slide to the bottom of the slope. You have to go up the slope again or wait for the lift to carry you.

Michael wants to know the longest bottom landslide in a region. The area is given by a two-dimensional array. Each number in the array represents the height of the point. Here is a sample

1  2  3  4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9

A person can slide from one point to the next, up or down one of the four points, 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


Problem Solving Ideas:

Memory search is more efficient than normal search. Have searched for no more search, there is DP thought. step[i][j] Save the current coordinate i,j can reach the maximum distance (not including yourself), for example 3 2 1, is currently 3, then the maximum distance to reach is 2, for each coordinate, can go up and down four directions to search, Take the largest distance, as the step[][of the coordinates].

The memory search here shows that when searching for a coordinate, the coordinates of the step[][] already have a value (searched), and is certainly optimal, then return directly to the step[][] value can be.

Code:

#include <iostream> #include <stdio.h> #include <string.h>using namespace Std;const int Maxn=110;int Mp[maxn][maxn];int step[maxn][maxn];int dx[4]={0,0,-1,1};int dy[4]={1,-1,0,0};int n,m;bool judge (int x,int y) {if (x&gt    ; =1&&x<=n&&y>=1&&y<=m) return true; return false;}    int dfs (int x,int y)//The maximum distance that the current position can reach {if (Step[x][y])//The search has no need to search again, directly return its value return step[x][y];        for (int i=0;i<4;i++) {int nextx=x+dx[i];        int nexty=y+dy[i];            if (judge (Nextx,nexty) &&mp[nextx][nexty]<mp[x][y]) {int Temp=dfs (nextx,nexty) +1;        if (Temp>step[x][y])//four direction to take the maximum value of step[x][y]=temp; }} return step[x][y];}    int main () {scanf ("%d%d", &n,&m);    memset (step,0,sizeof (step));    for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) scanf ("%d", &mp[i][j]);    int ans=0; for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) {           Step[i][j]=dfs (I,J);        if (Ans<step[i][j]) ans=step[i][j]; } printf ("%d", ans+1);//note +1, because itself is also a member of the sequence (the first) return 0;}


Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

[ACM] POJ 1088 ski (Memory search Dfs)

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.