HDOJ 题目1088 滑雪(DFS,记忆化)

滑雪

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 80448   Accepted: 29995

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

ac代码

#include<stdio.h>
#include<string.h>
#define max(a,b) (a>b?a:b)
int map[1010][1010],ans,n,m,dd[1010][1010];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int dfs(int x,int y)
{
	//ans=max(step,ans);
	if(dd[x][y])
		return dd[x][y];
	int maxn=0;
	for(int i=0;i<4;i++)
	{
		int tx,ty;
		tx=x+dx[i];
		ty=y+dy[i];
		if(tx>=0&&tx<n&&ty>=0&&ty<m&&map[x][y]>map[tx][ty])
		{
		//	vis[tx][ty]=1;
			int s;
			s=dfs(tx,ty);
		//	vis[tx][ty]=0;
			maxn=max(s,maxn);
		}
	}
	return dd[x][y]=maxn+1;
}
int main()
{
	//int n,m;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		int i,j;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
				scanf("%d",&map[i][j]);
		}
//		memset(vis,0,sizeof(vis));
		memset(dd,0,sizeof(dd));
		ans=0;
		for(i=0;i<n;i++)
		{
			for(j=0;j<m;j++)
			{
				//vis[i][j]=1;
				dd[i][j]=dfs(i,j);
				ans=max(ans,dd[i][j]);
				//vis[i][j]=0;
			}
		}
		printf("%d\n",ans);
	}
}
时间: 2024-08-05 11:27:43

HDOJ 题目1088 滑雪(DFS,记忆化)的相关文章

poj 1088 滑雪 【记忆化搜索】+【DFS】

策略:如题 题目链接:http://poj.org/problem?id=1088 代码: #include<stdio.h> #include<string.h> int map[105][105], dp[105][105], n, m; const int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; //四个方向 int limit(int x, int y) //判断是不是越界了 { if(x<1||x>n||y<1||

POJ 1088 滑雪(记忆化搜索)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 92384   Accepted: 34948 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

poj1088滑雪(dfs+记忆化搜索、备忘录)

题目信息: 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 一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小.在上面的例子中,一条可滑

OpenJ_Bailian - 1088 滑雪(记忆化搜索)

题意:给定一个二维数组,一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小,输出可以滑行的最长区域的长度. 分析:对于每一个点,进行记忆化搜索.若某点可以向四周某几个点滑行,记忆化搜索求出这几个可滑行点的最长滑行长度,取最大值,则该点的最长滑行长度为最大值+1. 注意:不能直接dp[x][y]=max(dp[x][y],dfs(tmpx,tmpy)+1),因为若四周没有可滑行的点,则该点最长滑行长度dp[x][y]会输出0,而实际dp[x][y]为1,分析中的方法可避免这个问题. #

poj 1088 滑雪【记忆化搜索】

题目链接:http://poj.org/problem?id=1088 基础题,不讲了,饿了 吃早饭去... 代码: #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #include <map> #include <string>

[ACM] POJ 1088 滑雪 (记忆化搜索复习)

滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 77763   Accepted: 28905 Description Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出.数组的每个数字代表点的高度.下面是一个例子 1 2 3 4 5 16 17

HDU 5024 Wang Xifeng&#39;s Little Plot (枚举 + DFS记忆化搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 513    Accepted Submission(s): 338 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

HDU 1484 Basic wall maze (dfs + 记忆化)

Basic wall maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 168    Accepted Submission(s): 52 Special Judge Problem Description In this problem you have to solve a very simple maze consisti

不要62 hdu 2089 dfs记忆化搜索

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意: 给你两个数作为一个闭区间的端点,求出该区间中不包含数字4和62的数的个数 思路: 数位dp中的 dfs 记忆化搜索方法解. 模板: int dfs(int i, int s, bool e) { if (i==-1) return s==target_s; if (!e && f[i][s] != -1) return f[i][s]; int res = 0; int u = e?