"Algorithmic Learning note" 55.DFS memory search SJTU OJ 1063 small m love ski

Source: Internet
Author: User

Description

Small m Super like skiing ~ ~ Skiing is really exciting. But in order to get the speed, the slippery area must be tilted downward, and when small m slips to the bottom of the slope, it will have to go up the slope again or wait for the lift to carry you. Little M wants to know the longest slide in the ski slopes. The ski slopes are given by a two-dimensional array. Each number of the array represents the relative distance of the point from the horizontal plane. 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

Small m can slide from one point to the next to the next 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 Format

The first line of input represents the number of rows in the range R and the number of columns C (1≤R,C≤ )。 Here are the r lines, each with a C integer representing the height h, −231−1≤h≤231 .

Output Format

The output line, an integer l, indicates the length of the longest slope of the ski slopes.

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
Hint

70% of data 1≤R,C≤

100% of data 1≤R,C≤

Adapted from SHTSC 2002

the wrong idea at first is that the longest path must be the longest path from the highest to the lowest, but that is not the case, because when the top point is tightly attached to a few lower points, it is similar to a big pit that is not going out. so we have to study each point as a starting point. when point A is the starting point, assuming that the longest path answer of the four points around a is already calculated, then the answer to a can be obtained by the state transfer equation. this equation meansThe longest path of a is the number of the four nodes around a that are smaller than it, and the result is the largest +1in order to avoid repetitive computations, the optimization can be accomplished by means of a memory search. The DFS process is quicker to write with a recursive returnIn addition this algorithm seems to be able to change to DP's writing, to sort and then from small to large, but not very good to ensure that every point calculation around the four have been calculated, the contradiction is very similar tohttp://www.cnblogs.com/yuchenlin/p/sjtu_oj_1358.htmlThe code is as follows:
#include <iostream>#include<cstring>#include<cstdio>using namespacestd;intR,c;//Number of rows and columnsintmap[ -+Ten][ -+Ten] = {0};//Record Heightintres[ -+Ten][ -+Ten] = {0};intdx[4] = {+1,-1,0,0};intdy[4] = {0,0,+1,-1};intAns =0;//Final Result//Initializevoidinit () {cin>>R>>C;  for(inti =1; I <= R; ++i) for(intj =1; J <= C; ++j) scanf ("%d",&Map[i][j]); } intDfsintXinty) {    if(Res[x][y] >0)//If you have calculated        returnRes[x][y]; intMax =0;  for(inti =0; I <4; ++i) {intNEWX = x +Dx[i]; intNewy = y +Dy[i]; //four points in the surrounding area than (x, y) short and the largest res as a follow-up to this point        if(Newx<=r and newx>=1and Newy<=c and newy>=1){            if(Map[newx][newy] <Map[x][y]) {                intTMP =DFS (Newx,newy); Max= TMP > Max?Tmp:max; }}} Res[x][y]= max +1; returnres[x][y];}intBuild () {intAns =0;  for(inti =1; I <= R; ++i) {         for(intj=1; J <= C; ++j) {ans= DFS (i,j) > ans?Res[i][j]: ans; }    }    returnans;}intMainintargcChar Const*argv[])     {init (); cout<<build () <<Endl; return 0;} 

"Algorithmic Learning note" 55.DFS memory search SJTU OJ 1063 small m love ski

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.