AcWing 901. ski

Address  https://www.acwing.com/problem/content/description/903/

Title Description
Given a matrix of R rows and C columns, showing a rectangular grid ski.

Dot matrix j-th column indicates the height of the i-th row j-th column of the i-th row region ski.

Starting from a person in a ski area, each unit may slide a distance in the vertical and horizontal directions in any one.

Of course, a person can slide into an adjacent area of ​​the premise is the height of the area below the height of the area of ​​their current location.

A matrix is ​​given below by way of 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

In a given matrix, a viable sliding track is 24-17-2-1.

In a given matrix, the longest glide trajectory 25-24-23- ... -3-2-1, along the way after a total of 25 regions.

Now given you a two-dimensional matrix represents the height of the regional ski resorts, you find the longest ski tracks in the ski area can be completed, and the output of its length (available through the maximum number of area).

Input format
The first line contains two integers R and C.

Next R rows, each row contains C integers, it represents a full two-dimensional matrix.

Output format
output an integer representing the maximum achievable length of the ski.

Data range
1≤R, C≤300,
0 ≦ matrix integer ≤10000

Sample input:
 . 5  . 5 
. 1  2  . 3  . 4  . 5 
16  . 17  18 is  . 19  . 6 
15  24  25  20 is  . 7 
14  23 is  22 is  21 is  . 8 
13 is  12 is  . 11  10  . 9 
Output Sample: 
25

Algorithm 1
a conventional DFS began to write
in some data timeout. Sure enough, not so simple

Code

 1 #include <iostream>
 2 #include <vector>
 3 
 4 using namespace std;
 5 
 6 const int N = 310;
 7 int n,m;
 8 int maxlen = -1;
 9 vector<vector<int>>  vv;
10 
11 int addx[] = {1,-1,0,0};
12 int addy[] = {0,0,1,-1 };
13  
14  void DFS ( int x, int y, int len)
 15  {
 16      os (len> maxlen) maxlen = len;
17  
18      for ( int i = 0 ; to < 4 ; to ++ ) {
 19          int newx = x + addx [i];
20          int OF NEW = the + addy [i];
21  
22          os (newx> = 0 && newx <n && OF NEW> = 0 && OF NEW <m &&
 23              vv [newx] [OF NEW] < vv[x][y])
24         {
25             dfs(newx,newy,len+1);        
26         }
27     }
28 }
29 
30 
31 int main()
32 {
33     cin >> n >> m;
34 
35     for(int i = 0; i < n;i++){
36         vector<int> v;
37         vv.push_back(v);
38         for(int j = 0;j< m;j++){
39             int t =0;
40             cin >> t;
41             vv[i].push_back(t);    
42         }
43     }
44 
45 
46     for(int i = 0; i < n;i++){
47         for(int j = 0;j < m;j++){
48             int len =0;
49             dfs(i,j,len);
50         }
51     }
52 
53     cout << maxlen+1  << endl;
54 
55     return 0;
56 }
View Code

2 algorithm
using memory array of recorded maximum sliding length at each point
through each point as the starting point
and the detection point if that point can be slid around to other points
then the maximum sliding length of the sliding point is a point other can slide length + 1'd
DP [I] [J] = max (DP [I] [J-. 1], DP [I] [J +. 1], DP [. 1-I] [J], DP [I +. 1] [ j])

Because skiing is to be lower than the current slide in dfs any point of a starting point, there will be no question of a multiple entry points
if dp that point [] [] is not initialized then that does not have to calculate the value calculated again .

According to the above equation the following code AC

#include <iostream>

using namespace std;

const int N = 310;
int R,C;

int arr[N][N];
int dp[N][N];

int maxlen = -1;

int addx[] = {1,-1,0,0};
int addy[] = {0,0,1,-1};

int  Dfs(int x,int y)
{
    if(dp[x][y] != 0) return dp[x][y];


     for(int i = 0;i < 4;i++){
        int newx = x + addx[i];
        int newy = y + addy[i];

        if(newx >=0 && newx < C && newy >=0 && newy < R &&
            arr[newx][newy] < arr[x][y])
        {
            dp[x][y] = max(dp[x][y], Dfs(newx,newy) +1);
        }
    }
    return dp[x][y];
}

int main()
{
    ios::sync_with_stdio(false);

  cin >> R >>C;

  for(int i = 0; i < R;i++){
      for(int j=0;j<C;j++){
        cin >> arr[i][j];    
      }
  }

  for(int i = 0; i < R;i++){
      for(int j = 0;j< C;j++){
        int len = Dfs(i,j);   
        maxlen = max(maxlen,len);
      }
  }  

  cout << maxlen +1  << endl;

    return 0;
}

 

Guess you like

Origin www.cnblogs.com/itdef/p/11668749.html