"Dynamic planning" Skiing_ deep Search _ Dynamic Planning

Source: Internet
Author: User

Issue B: "Dynamic planning" skiing time limit: 1 Sec memory limit: MB
Submitted by: 11 Resolution:
Submitted State [Discussion Version] Title 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 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 represents several sets of test data, and the second line of input represents the number of rows R and the number of columns C (1 <= r,c <= 100) of the range. The following are the r lines, each with a C integer representing a height of h,0<=h<=10000.
followed by the next set of data;

Output

The length of the longest region of the output.

Sample input
15 51 2 3 4 516 17 18 19 615 24 25 20 714 23 22 21 813 12 11 10 9
Sample output
25

Solution: set H[I][J] stores the maximum length, traversing each point (because each point can be the end of the longest region.) ) Use the DFS () function for each point;
Previously done is linear, and then two-dimensional, so you need to set up a Dfs () function, by depth first search. Select A[i][j] up and down four in the smaller than he, the height of the largest storage, and then +1;
State transfer equation: H[i][j]=max (h[i][j], (h[i-1][j],h[i+1][j],h[i][j-1],h[i][j+1]) +1);
Code:
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 intN;7 intr,c;8 intans;9 intaa[ the][ the];Ten inth[ the][ the]; One  A intDfsintAintb) { -     intdir_x[4]={-1,1,0,0};//Up or down -     intdir_y[4]={0,0,-1,1};//Up or down the      for(intI=0;i<4; i++){ -         if(h[a][b]!=0&&aa[a+dir_x[i]][b+dir_y[i]]<Aa[a][b]) { -H[a][b]=max (H[a][b],dfs (a+dir_x[i],b+dir_y[i]) +1); -         } +     } -     returnH[a][b]; + } A  at intMain () - { -scanf"%d",&n); -      while(n--){ -scanf"%d%d",&r,&c); -          for(intI=1; i<=r;i++){ in              for(intj=1; j<=c;j++){ -scanf"%d",&aa[i][j]); toh[i][j]=1; +             } -         } the          for(intI=0; i<=n;i++){ *h[i][0]=0; $h[0][i]=0;Panax Notoginseng         } -ans=0; the          for(intI=1; i<=r;i++){ +              for(intj=1; j<=c;j++){ Aans=Max (Ans,dfs (i,j)); the             } +         } -printf"%d\n", ans); $  $  -     } -  the  - Wuyi  the  -     return 0; Wu}



"Dynamic planning" Skiing_ deep Search _ Dynamic Planning

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.