uva10285 longest Run on a Snowboard (DP's memory search)

Source: Internet
Author: User

10285 longest Run on a Snowboard
Michael likes snowboarding. That's not very surprising, since snowboarding is really great. The bad
Thing is, and the area must slide downwards. Another disadvantage is the when
You've reached the bottom of the hill you had to walk up again or wait for the ski-lift.
Michael would like-to-know how long the longest run was in. That area was given by a grid of
Numbers, defining the heights at those points. Look at this 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 down from one point to a connected other one if and only if the height decreases. One
Point was connected to another if it's at left, at right, above or below it. In the sample map, a possible
Slide would is 24-17-16-1 (start at @, end at 1). Of course if you would go 25-24-23-...-3-2-1, it would
Be a much longer run. In fact, it ' s the longest possible.
Input
The first line contains the number of test cases N. Each test case starts with a line containing the
Name (it s a single string), the number of the rows R and the number of columns C. After that follow R
Lines with C numbers each, defining the heights. R and C won ' t is bigger than, N not bigger than
The heights is always in the range from 0 to 100.
Output
For each test case, print a line containing the name of the area, a colon, a space and the length of the
Longest run one can slide down in the that area.
Sample Input
2
Feldberg 10 5
56 14 51) 58 88
26 94 24) 39 41
24 16 8) 51 51
76 72 77) 43 10
38 50 59) 84 81
5 23 37) 71 77
96 10 93) 53 82
94 15 96) 69 9
74 0 62) 38 96
37 54 55) 82 38
Spiral 5 5
1 2 3) 4 5
16 17 18) 19 6
15 24 25) 20 7
Universidad de Valladolid Oj:10285–longest Run on a Snowboard 2/2
14 23 22) 21 8
13 12 11) 10 9
Sample Output
Feldberg:7
Spiral:25

Memory Search

/*solve:*/#include <iostream>#include <algorithm>#include <map>#include <cstdio>#include <cstdlib>#include <vector>#include <cmath>#include <cstring>#include <stack>#include <string>#include <set>#include <fstream>using namespace STD;#define PB Push_back#define CL (A, B) memset (A,b,sizeof (a) )#define BUG printf ("===\n");#define REP (A, b) for (int i=a;i<b;i++)#define REP_ (A, b) for (int i=a;i<=b;i++)#define P pair<int,int>#define X First#define Y Second#define VI vector<int>Const intmaxn= the;Const intinf=999999999;typedef Long LongLL;voidMax (int&a,intb) {if(a<b) a=b;}Charname[ -];intA[MAXN][MAXN];intDP[MAXN][MAXN];//end with (i,j) you can get the max lengthintN,m;intdx[]={0,0,1,-1};intdy[]={1,-1,0,0};intDfsintXintY) {//Indicates the maximum length to be reached from this point (x, y)    if(Dp[x][y]) {returnDp[x][y]; }intans=0; for(intI=0;i<4; i++) {intXx=dx[i]+x;intYy=dy[i]+y;if(xx>=1&&xx<=n&&yy>=1&&yy<=m&&a[xx][yy]<a[x][y]) {Ans=max (Ans,dfs (XX,YY));//Record the largest of the four directions starting at this point}} dp[x][y]+=ans+1;//maximum plus the current point    returnDp[x][y];}intMain () {intTscanf("%d", &t); while(t--) {scanf("%s%d%d", name,&n,&m);intmxv=0, PI,PJ; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {scanf("%d", &a[i][j]);if(A[I][J]&GT;MXV)                    {MXV=A[I][J];                    Pi=i;                Pj=j; }}} cl (DP,0);intmx=1; for(intI=1; i<=n;i++) { for(intj=1; j<=m;j++) {Mx=max (Mx,dfs (i,j));//Traverse every point}        }printf("%s:%d\n", NAME,MX); }return 0;}/*2feldberg 10 556 14 51 58 8826 94 24 39 4124 16 8 51 5176 72 77 43 1038 50 59 84 815 23 37 71 7796 10 93 53 8294 15 974 0 9637-38Spiral 5 2 3 4 516-615 (714) 813 (9*/-Ten)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

uva10285 longest Run on a Snowboard (DP's memory search)

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.