动态规划-poj1088 滑雪 ---增加了最优路径构建


一、有两种思路:

1)根据记忆搜索(保存了最优值表格),在纯粹的递归思路里加入记忆表格。

2)先将高度排序,然后依次从点由低到高 计算 该点的最长路径长度。(因为当矮点不可能通过高点,而比当前矮的点都在之前计算好了)

--------------------------------------------

二、关于最优路径构建

将点的最大路径长度依赖的点的(dx,dy) 即 行、列的增量存储在一个表格中。

Language: Default
滑雪
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 50449 Accepted: 18324

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

#include <stdio.h>
int r,c;
//#define MAX 102
int height[102][102];
int length[102][102];

int FindMaxLength(int i,int j)
{
if(length[i][j]>0)
return length[i][j];
int tmp1=0;
int tmp2=0;
int tmp3=0;
int tmp4=0;
//left
if(j>=2)
if(height[i][j-1]<height[i][j])
tmp1=1+FindMaxLength(i,j-1);
//right
if(j<=c-1)
if(height[i][j+1]<height[i][j])
tmp2=1+FindMaxLength(i,j+1);
//up
if(i>=2 )
if( height[i-1][j]< height[i][j])
tmp3=1+FindMaxLength(i-1,j);
//down
if(i<=r-1 )
if(height[i+1][j]<height[i][j])
tmp4=1+FindMaxLength(i+1,j);

int max=0;
if(tmp1>max)
{
max=tmp1;
}
if(tmp2>max)
{
max=tmp2;
}
if(tmp3>max)
{
max=tmp3;
}
if(tmp4>max)
{
max=tmp4;
}
return max;
}
int main()
{

scanf("%d",&r);
scanf("%d",&c);
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
scanf("%d",&height[i][j]);
length[i][j]=0;
}
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
length[i][j]=FindMaxLength(i,j);
}
int max=0;
for(int i=1;i<=r;i++)
for(int j=1;j<=c;j++)
{
if(length[i][j]>max)
{
max=length[i][j];
}
}
max=max+1;
printf("%d\n",max);
scanf("%d",&r);
return 0;
}



增加 最优路径的代码:#include <stdio.h> int m,n; #define MAX 102 int height[MAX][MAX]; int length[MAX][MAX]; typedef struct r { int dx; int dy; }; r route[MAX][MAX]; //dx,dy int tmp1,tmp2,tmp3,tmp4; int MaxLength(int i,int j) { if(length[i][j]>0) return length[i][j]; tmp1=tmp2=tmp3=tmp4=0; //left if(j>=1 && height[i][j-1]<height[i][j]) tmp1=1+MaxLength(i,j-1); //right if(j<=n-2 && height[i][j+1]<height[i][j]) tmp2=1+MaxLength(i,j+1); //up if(i>=1 && height[i-1][j]< height[i][j]) tmp3=1+MaxLength(i-1,j); //down if(i<=m-2 && height[i+1][j]<height[i][j]) tmp4=1+MaxLength(i+1,j); int max=0; if(tmp1>max) { max=tmp1; route[i][j].dx=0; route[i][j].dy=-1; } if(tmp2>max) { max=tmp2; route[i][j].dx=0; route[i][j].dy=1; } if(tmp3>max) { max=tmp3; route[i][j].dx=-1; route[i][j].dy=0; } if(tmp4>max) { max=tmp4; route[i][j].dx=1; route[i][j].dy=0; } length[i][j]=max; return max; } int main() { scanf("%d%d",&m,&n); for(int i=0;i<m;i++) for(int j=0;j<n;j++) { scanf("%d",&height[i][j]); length[i][j]=0; route[i][j].dx=0;//dx route[i][j].dy=0;//dy } for(int i=0;i<m;i++) for(int j=0;j<n;j++) { MaxLength(i,j); } int max=0; int min=100000000; int posR=-1,posC=-1; for(int i=0;i<m;i++) for(int j=0;j<n;j++) { if(length[i][j]>max) { max=length[i][j]; posR=i; posC=j; } // if(length[i][j]<min) // min=length[i][j]; } printf("%d\n",max); printf("max length:%d\n,posR:%d\n posC:%d\n",max,posR,posC); for(int i=0;i<m;i++) for(int j=0;j<n;j++) { printf("%d, %d\n",route[i][j].dx,route[i][j].dy); printf("height :%d\n",length[i][j]); } while(1) { if(route[posR][posC].dx==0 && route[posR][posC].dy==0) { printf("%d\n",height[posR][posC]); break; } printf("%d\n",height[posR][posC]); int tmp11,tmp22; tmp11=posR+route[posR][posC].dx; tmp22=posC+route[posR][posC].dy; posR=tmp11; posC=tmp22; } scanf("%d",&n); return 0; }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值