POJ-1088-滑雪


Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 93221
Accepted: 35289

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

解题思路:这道题用的是记忆化搜索的思想,也就是 搜索 + 动态规划,把每次搜索的结果都存起来,等用到的时候就调用数组里面的值,所以时间效率要比普通的搜索高很多。

ps:动态规划是由下而上的,通俗的说就是先求出一个小状态的最优解,通过这个小状态的最优解一步一步的求出整个状态的最优解,而记忆化搜索是先求整个状态的最优解,在求这个最优解的过程中一定会用到小状态的最优解,而求这个小状态的最优解时,就是一步步递归的过程。只是我自己理解他们的区别, 不足之处请指出

大神对记忆化搜索的解释:http://blog.csdn.net/urecvbnkuhbh_54245df/article/details/5847876



#include
#include
#include
using namespace std;
int direction[4][2] = {0,-1,-1,0,1,0,0,1};
int map[105][105];
int dp[105][105];
int row,col;
int DFS(int map_x,int map_y)
{
if(dp[map_x][map_y] != -1){
return dp[map_x][map_y];
}
int cnt_l=1;
for(int i=0; i<4; i++){
int re_mx = map_x + direction[i][0];
int re_my = map_y + direction[i][1];
if(re_mx >= row || re_my >= col)
continue;
else if(re_mx < 0 || re_my < 0)
continue;
else{
if(map[re_mx][re_my] > map[map_x][map_y]){
cnt_l = max(DFS(re_mx,re_my)+1,cnt_l);
}
}
}
dp[map_x][map_y] = cnt_l;
return cnt_l;
}
int main()
{
while(~scanf("%d%d",&row,&col)){
for(int i=0; i for(int j=0; j scanf("%d",&map[i][j]);
}
}
int long_m=0;
memset(dp,-1,sizeof(dp));
for(int i=0; i for(int j=0; j long_m = max(long_m,DFS(i,j));
}
}
printf("%d\n",long_m);
}
return 0;
}

你可能感兴趣的:(动态规划)