POJ 1088 滑雪

POJ_1088

一开始没有什么思路,看了别人的代码后才发现原来这个题目就是一个动态规划的题目,主要的过程用记忆化递归来实现。

在记录好各点的高度后,只需要依次枚举每个点,如果该点的dis[][]已被赋值,那么就直接将其与maxdis进行比较,之后maxdis更新为较大值,如果该点的dis[][]没有被赋值,那么就以该点为基础,向周围四个方向中可达的方向进行深搜,之后将dis[][]记录为这几个方向中的最大值+1,再将dis[][]的值与maxdis进行比较。

#include<stdio.h>
#include
<string.h>
int height[110][110],dis[110][110],r,c;
int dfs(int i,int j)
{
int temp,max;
if(dis[i][j]>0)
return dis[i][j];
max
=1;
if(i>0&&height[i-1][j]>height[i][j])
{
temp
=dfs(i-1,j)+1;
if(temp>max)
max
=temp;
}
if(i<r-1&&height[i+1][j]>height[i][j])
{
temp
=dfs(i+1,j)+1;
if(temp>max)
max
=temp;
}
if(j>0&&height[i][j-1]>height[i][j])
{
temp
=dfs(i,j-1)+1;
if(temp>max)
max
=temp;
}
if(j<c-1&&height[i][j+1]>height[i][j])
{
temp
=dfs(i,j+1)+1;
if(temp>max)
max
=temp;
}
return dis[i][j]=max;
}
int main()
{
int i,j,maxdis,temp;
while(scanf("%d%d",&r,&c)==2)
{
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf(
"%d",&height[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
dis[i][j]
=0;
maxdis
=0;
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
temp
=dfs(i,j);
if(temp>maxdis)
maxdis
=temp;
}
printf(
"%d\n",maxdis);
}
return 0;
}

  

原文地址:https://www.cnblogs.com/staginner/p/2146379.html