poj 1088 滑雪

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
记忆化搜索,其实和DP是一个道理
#include<iostream>
#include<stdio.h>
using namespace std ;
int n, m,re,minn,minnx,minny;
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}},map[105][105],dis[105][105],front;
int fun(int a)
{
	if(a<0)
		return -a;
	return a;
}
int dfs(int i,int j,int step)
{
	if(dis[i][j]!=-1)
	{
		if(step+dis[i][j]>re)
			re=step+dis[i][j];
        if(step+dis[i][j]>front)
        front=step+dis[i][j];
		return step+dis[i][j];
	}

	bool zhe=true;
	int d,x,y;

	for(d=0;d<4;d++)
	{
		x=i+dir[d][0];
		y=j+dir[d][1];
		if((x>=0)&&(x<n)&&(y>=0)&&(y<m)&&map[x][y]<map[i][j])
		{

			dfs(x,y,step+1);
			zhe=false;
		}
	}
	if(zhe)
	{

		if(step>front)
		front=step;
		if(step>re)
		re=step;
		if(re<step)
		{

			re=step;

		}

	}
	return step;

}
int main ()
{
	int i,j;
	while(scanf("%d%d",&n,&m)!=EOF)
	{


		minn=0x4f4f4f4f;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
				scanf("%d",&map[i][j]);

				if(map[i][j]<minn)
				{
					minn=map[i][j];
					minnx=i;
					minny=j;
				}
				dis[i][j]=-1;

			}
			re=-1;
		for(i=0;i<n;i++)
			for(j=0;j<m;j++)
			{
			    front=-1;
				int k=dfs(i,j,0);

               // printf("%d dfs%d 
",front,re);
				if(front!=-1)
					dis[i][j]=front;


			}
			printf("%d
",re+1);

	}

	return 0;
}


原文地址:https://www.cnblogs.com/snake-hand/p/3172269.html