洛谷P1434 [SHOI2002]滑雪
生活随笔
收集整理的這篇文章主要介紹了
洛谷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 代碼轉載于:https://www.cnblogs.com/handsomegodzilla/p/11295278.html
總結
以上是生活随笔為你收集整理的洛谷P1434 [SHOI2002]滑雪的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019牛客暑期多校训练营(第五场)-
- 下一篇: 洛谷P2068 统计和题解