Code[vs] 2152 Ski puzzle

Source: Internet
Author: User

Code[vs] 2152 ski Title Description Description

TRS likes to ski. He came to a ski resort where the ski was a rectangle, and for the sake of simplicity, we used the matrix of the R row C column to represent each piece of terrain. To get a faster speed, the glide route must be tilted downward.
For example, the rectangle in the sample, you can slide from one point to the top or bottom four adjacent points. For example 24-17-16-1, in fact 25-24-23 ... 3-2-1 longer, in fact this is the longest one.

Enter a description input Description

Input file

Line 1th: Two digits r,c (1<=r,c<=100), representing the rows and columns of a matrix.
2nd.. R+1 Line: The number of c per line, indicating the matrix.

outputs description output Description

Output file

Only one row: outputs 1 integers representing the maximum length that can be slid.

sample input to sample

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 Sample outputs

25

data size & Hint

1s

———————————————————————————— Split Line ————————————————————————————

The beginning of this problem, then think of the method of memory Dfs, due to the small data, can easily. However, when the data is a little bit larger, then recursion is not appropriate. So, how to solve this problem with a non-recursive method? Observe the problem carefully and find the problem of longest ascending subsequence sequenceIt is somewhat similar to the subject, how to Turning Two-dimensional problems into one-dimensional problemsis the key to solving problems. We put each mountain in accordance with the height of the order into a one-dimensional array, and record the original coordinates, then the longest ascending sub-sequence, only in which, add two mountains whether adjacent to the judgment can be. This method avoids a stack overflow that is caused by too deep a recursive layer when the data is large in size.  It is also very easy to understand. The code is as follows:

1 //Code by DRSHHHS2 3#include"bits/stdc++.h"4 5 using namespacestd;6 7 Const intMAXN =10010 ;8 Const intINF =2147483647 ;9 Ten structSlide {intx, Y, Val;}; One  A Slide ARR[MAXN]; - intF[MAXN]; -  the BOOLCMP (Slide A, Slide b) {returnA.val >B.val;} - intAbs (intx) {returnX>0? x:-x;} - intMax (intAintb) {returnA>b?a:b;} -  + BOOLJudge (intP1,intP2) {//determine whether the two mountains are adjacent and down Strictly -          if(ABS (arr[p1].x-arr[p2].x) + ABS (ARR[P1].Y-ARR[P2].Y)) = =1&& arr[p2].val > Arr[p1].val)return true ; +          Else                                                                                     return false ; A } at  - intMain () { -          intN, M, tmp, K =0, ans =-INF; -           -scanf"%d%d", &n,&M); -           for(intI=1; I<=n; ++i) { in                     for(intj=1; J<=m; ++j) { -k++; toscanf"%d", &tmp);//reads a two-dimensional array into a one-dimensional array in arr +Arr[k].val =tmp; -arr[k].x = i; arr[k].y = j;//Record original coordinates the                    } *          } $          Panax NotoginsengSort (arr+1, arr+k+1, CMP);//Sort -           thef[1] =1 ; +  A           for(intI=2; I<=k; ++i) {//Longest descending sub-sequence the                     for(intj=1; j<=i-1; ++j) { +                              if(Judge (I, J)) {//Judging the adjacent cut down Strictly -F[i] =Max (F[i], f[j]); $                              }      $                    } -F[i] = F[i] +1 ;  -          } the  -           for(intI=1; I<=k; ++i) {//find the maximum valueWuyiAns =Max (F[i], ans); the          } -           Wuprintf ("%d", ans); -           About          return 0 ; $}
PS: This problem is visualized as the extension of the longest descending subsequence in two dimensions. By the way, the code for the memory search is attached as follows:
1#include"bits/stdc++.h"2 3 using namespacestd;4 Const intMAXN = the ;5 6 intH[MAXN][MAXN], F[MAXN][MAXN];7 intN, m, ans =1;8 9 intDFS (intXinty)Ten { One     if(F[x][y])returnF[x][y]; AF[x][y] =1; -     if(X >1&& H[x][y] < h[x-1][y]) F[x][y] = max (F[x][y], DFS (X-1, y) +1); -     if(Y >1&& H[x][y] < h[x][y-1]) F[x][y] = max (F[x][y], DFS (x, Y-1) +1); the     if(x < n && H[x][y] < H[x +1][y]) F[x][y] = max (F[x][y], DFS (x +1, y) +1); -     if(Y < M && H[x][y] < H[x][y +1]) F[x][y] = max (F[x][y], DFS (x, y +1) +1); -Ans =Max (ans, f[x][y]); -     returnF[x][y]; + } - intMain () + { Ascanf"%d%d", &n, &m); at      for(inti =1; I <= N; i++ ) -          for(intj =1; J <= M; J + + ) -scanf"%d", &h[i][j]); -      for(inti =1; I <= N; i++ ) -          for(intj =1; J <= M; J + + ) -F[I][J] =DFS (i, j); inprintf"%d", ans); -  to     return 0; +}

2016-09-14 15:45:58

Finish

Code[vs] 2152 Ski puzzle

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.