poj2106,poj 1088(記憶化搜索)

 2023-10-06 阅读 29 评论 0

摘要:滑雪 Time Limit: 1000MS?Memory Limit: 65536KTotal Submissions: 88560?Accepted: 33212 Description Michael 喜歡滑雪百這并不奇怪, 因為滑雪的確很刺激。可是為了獲得速度,滑的區域必須向下傾斜,而且當你滑到坡底,你不得不再次走上坡或者等待
滑雪
Time Limit: 1000MS?Memory Limit: 65536K
Total Submissions: 88560?Accepted: 33212

Description

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。當然25-24-23-...-3-2-1更長。事實上,這是最長的一條。

Input

輸入的第一行表示區域的行數R和列數C(1 <= R,C <= 100)。下面是R行,每行有C個整數,代表高度h,0<=h<=10000。

Output

輸出最長區域的長度。

Sample Input

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

Sample Output

25

Source

SHTSC 2002

以前真是太水了。。竟然是看了解題報告覺得好神奇 前幾個月的咸魚,現在叫菜鳥吧2333

重寫一遍,一個記憶化搜索的基礎題。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
const int N = 105;
int mp[N][N];
int dp[N][N]; ///dp[i][j]保存的是當前點能劃最遠的距離
int n,m;
int dir[][2] ={{1,0},{-1,0},{0,1},{0,-1}};
int dfs(int x,int y){int ans = 0;if(dp[x][y]>1) return dp[x][y];  ///記憶化搜索:已經搜過剪枝for(int i=0;i<4;i++){int nextx = x+dir[i][0];int nexty = y+dir[i][1];if(nextx<1||nextx>n||nexty<1||nexty>m) continue;if(mp[nextx][nexty]<mp[x][y]){ans = max(ans,dfs(nextx,nexty));}}dp[x][y]=ans+1; ///子問題的解加上1就是當前點的解return dp[x][y];
}
int main()
{while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;i++)for(int j=1;j<=m;j++){scanf("%d",&mp[i][j]);dp[i][j] = 1;   ///初始化每個點的高度為自身
        }int ans = -1;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){dfs(i,j);if(dp[i][j]>ans) ans = dp[i][j];}}printf("%d\n",ans);}}

?

轉載于:https://www.cnblogs.com/liyinggang/p/5384343.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/119583.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息