Luogu P1434 [SHOI2002] Skiing

Keywords: C++ less

Topic Description

Michael likes skiing. It's not surprising, because skiing is really exciting. But in order to get speed, the sliding area has to tilt downward, and when you slide to the bottom of the slope, you have to go up again or wait for the elevator to pick you up. Michael wants to know the longest landslide in an area. The region is given by a two-dimensional array. Each number 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

A person can slide up and down from a point to one of the four adjacent points, if and only if the height decreases. In the above example, a feasible landslide is 24-17-16-1 (starting from 24 and ending at 1). Of course 25-24-23-3-2-1 is longer. In fact, this is the longest one.

Input format

The first behavior of the input represents the row number R and column number C (1 < R, C < 100) of a two-dimensional array of regions. Below are R rows, each with a number of C, representing the height (one space between two numbers).

Output format

The length of the longest landslide in the output area.

Input and Output Samples

Input #1 replication
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
Output #1 replication
25
Analysis:

Memorized Search
The input is an array of g.
The f array is used to record the answers.
Initially, f arrays are initialized to 1
Then the double loop starts searching every point.
Attention should be paid to the condition that the limit can only be slipped from large to small, that is strictly less than, and that the maximum value can be sought.

Explosive Search Can Get 90pts Good Achievements

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<iomanip>
 8 #include<cstdlib>
 9 #include<queue>
10 #include<set>
11 #include<map>
12 #include<stack>
13 #include<vector>
14 #define re register
15 #define Max 110
16 #define D double
17 #define gc getchar
18 inline int read(){
19     int a=0;int f=0;char p=gc();
20     while(!isdigit(p)){f|=p=='-';p=gc();}
21     while(isdigit(p)){a=a*10+p-'0';p=gc();}
22     return f?-a:a;
23 }
24 int n,m,g[Max][Max],ans=1;
25 bool vis[Max][Max]={0};
26 void dfs(int x,int y,int step)
27 {
28     ans=std::max(ans,step);
29     if(x-1>=1 && g[x-1][y]<g[x][y] && vis[x-1][y]==0)
30         vis[x-1][y]=1,dfs(x-1,y,step+1),vis[x-1][y]=0;
31     if(x+1<=n && g[x+1][y]<g[x][y] && vis[x+1][y]==0)
32         vis[x+1][y]=1,dfs(x+1,y,step+1),vis[x+1][y]=0;
33     if(y-1>=1 && g[x][y-1]<g[x][y] && vis[x][y-1]==0)
34         vis[x][y-1]=1,dfs(x,y-1,step+1),vis[x][y-1]=0;
35     if(y+1<=m && g[x][y+1]<g[x][y] && vis[x][y+1]==0)
36         vis[x][y+1]=1,dfs(x,y+1,step+1),vis[x][y+1]=0;
37 }
38 int main()
39 {
40     n=read();m=read();
41     for(re int i = 1 ; i <= n ; ++ i)
42         for(re int j = 1 ; j <= m ; ++ j)
43             g[i][j]=read();
44     for(re int i = 1 ; i <= n ; ++ i)
45         for(re int j = 1 ; j <= m ; ++ j)
46             dfs(i,j,1);
47     printf("%d",ans);
48     return 0;
49 }
90 minutes explosive search
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<algorithm>
 7 #include<iomanip>
 8 #include<cstdlib>
 9 #include<queue>
10 #include<set>
11 #include<map>
12 #include<stack>
13 #include<vector>
14 #define re register
15 #define Max 110
16 #define D double
17 #define gc getchar
18 inline int read()
19 {
20     int a=0;int f=0;char p=gc();
21     while(!isdigit(p)){f|=p=='-';p=gc();}
22     while(isdigit(p)){a=a*10+p-'0';p=gc();}
23     return f?-a:a;
24 }
25 int n,m,g[Max][Max],ans=1,f[Max][Max];
26 int dfs(int x,int y)
27 {
28     if(f[x][y]!=1) return f[x][y];int t=0;
29     if(x-1>=1 && g[x-1][y]<g[x][y])
30         t=std::max(dfs(x-1,y)+1,t);
31     if(x+1<=n && g[x+1][y]<g[x][y])
32         t=std::max(dfs(x+1,y)+1,t);
33     if(y-1>=1 && g[x][y-1]<g[x][y])
34         t=std::max(dfs(x,y-1)+1,t);
35     if(y+1<=m && g[x][y+1]<g[x][y])
36         t=std::max(dfs(x,y+1)+1,t);
37     f[x][y]=std::max(f[x][y],t);
38     return f[x][y];
39 }
40 int main()
41 {
42     n=read();m=read();
43     for(re int i = 1 ; i <= n ; ++ i)
44         for(re int j = 1 ; j <= m ; ++ j)
45             g[i][j]=read(),f[i][j]=1;
46     for(re int i = 1 ; i <= n ; ++ i)
47         for(re int j = 1 ; j <= m ; ++ j)
48                 ans=std::max(ans,dfs(i,j));
49     printf("%d",ans);
50     return 0;
51 }
AC code

Posted by MarcAndreTalbot on Sat, 03 Aug 2019 01:52:35 -0700