洛谷P1434 [SHOI2002]滑雪

题目描述

Michael喜欢滑雪。这并不奇怪,因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子:

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

一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可行的滑坡为24-17-16-1(从24开始,在1结束)。当然25-24-23-...-3-2-1更长。事实上,这是最长的一条。

输入格式

输入的第一行为表示区域的二维数组的行数R和列数C(1≤R,C≤100)。下面是R行,每行有C个数,代表高度(两个数字之间用1个空格间隔)。

输出格式

输出区域中最长滑坡的长度。

输入输出样例

输入 #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
输出 #1
25
解析:

记忆化搜索
输入的是g数组
在记录答案时使用的是f数组
一开始f数组都初始化为1
然后两重循环从每一个点都开始搜一遍
注意限定条件是只能从大的滑向小的,是严格小于,寻求最大值。

爆搜可以得到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分爆搜
 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 代码

猜你喜欢

转载自www.cnblogs.com/handsomegodzilla/p/11295278.html