Luo Gu P1434 [SHOI2002] Skiing

Title Description

Michael likes to ski. This is not surprising, because skiing is very exciting. However, in order to gain speed, slippery areas must be downward, and when you slide into the base, you have to up the grade again or wait for the elevator to take you. Michael wanted to know the longest in a landslide area. Region is given by a two-dimensional array. Each number represents the height of the point of the array. Below 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 adjacent to one of the four points, if and only if the height is reduced. In the above example, a feasible landslide 24-17-16-1 (from 24 starts, at the end of 1). Of course 25-24-23 -...- 3-2-1 longer. In fact, this is the longest one.

Input Format

The first line represents a two-dimensional array of input areas of the number of rows R and columns C (1≤R, C≤100). The following is R rows, each row having the number of C, representative of the height (with a space interval between two digits).

Output Format

The longest length of the output area of ​​the landslide.

Sample input and output

Input # 1
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
25 
Resolution:

Memory search
input is g array
used in the recording when the answer is f array
beginning f the array are initialized to 1
and then double cycle from each search start point again
noted with the proviso that only little from a large slide It is strictly less than to seek the maximum.

Burst search can get good results in 90pts

 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 minute burst 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

Guess you like

Origin www.cnblogs.com/handsomegodzilla/p/11295278.html