POJ 1088 滑雪 (深搜)

题目链接:http://poj.org/problem?id=1088

题面:

滑雪

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 83760   Accepted: 31349

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

题目大意:

可以从任意点出发,求一条最长的递减路径的长度。

解题:

直接搜索是肯定会TLE的。可以记录从某点出发的最长递减路径,如果该点的值已经算过了,那么直接取就可以了。时间复杂度貌似算不来,不过看程序跑得还行,32ms。

代码:

#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
//行数、列数、高度、记录结果
int row,col,height[105][105],ans[105][105];
//4个方向
int dir[4][2]={0,1,-1,0,0,-1,1,0};
int max(int a,int b)
{
	return a>b?a:b;
}
//判断是否越界
bool inside(int x,int y)
{
	if(x<0||x>=row||y<0||y>=col)
		return false;
	else
	    return true;
}
//搜索过程
int dfs(int x,int y)
{
	int tx,ty;
	if(ans[x][y])
		return ans[x][y];
	//4个方向移动
	for(int i=0;i<4;i++)
	{
		tx=x+dir[i][0];
		ty=y+dir[i][1];
		//+1是因为算上当前格
		if(inside(tx,ty)&&height[tx][ty]<height[x][y])
		  ans[x][y]=max(ans[x][y],dfs(tx,ty)+1);
	}
	return ans[x][y];
}
int main()
{
   int maxn;
   while(~scanf("%d%d",&row,&col))
   {
   	  maxn=1;
	  memset(ans,0,sizeof(ans));
   	  for(int i=0;i<row;i++)
  	   	for(int j=0;j<col;j++)
	   	  scanf("%d",&height[i][j]);
      for(int i=0;i<row;i++)
      {
      	for(int j=0;j<col;j++)
      	{
	      ans[i][j]=dfs(i,j);
          maxn=max(maxn,ans[i][j]+1);
		}
      }
      printf("%d\n",maxn);
   }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-26 07:54:17

POJ 1088 滑雪 (深搜)的相关文章

POJ 1088 滑雪 (动规)

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

POJ 1088 滑雪 记忆化优化题解

本题有人写是DP,不过和DP还是有点差别的,应该主要是记忆化 Momoization 算法. 思路就是递归,然后在递归的过程把计算的结果记录起来,以便后面使用. 很经典的搜索题目,这种方法很多题目考到的. 关键还是如何把代码写清晰工整了,O(∩_∩)O~. #include <stdio.h> const int MAX_N = 101; int R, C; int arr[MAX_N][MAX_N]; int tbl[MAX_N][MAX_N]; inline int max(int a,

poj 1088 滑雪

http://poj.org/problem?id=1088 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int g[200][200]; 7 int dp[200][200]; 8 int r,c; 9 int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; 10 int max1=-1; 11 12

[ACM] poj 1088 滑雪 (记忆化搜索DFS)

求n*m网格内矩形的数目[ACM] poj 1088 滑雪 (记忆化搜索DFS),布布扣,bubuko.com

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 滑雪(DP)

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

POJ 1088 滑雪(记忆化搜索)

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

POJ 1088 滑雪 记忆化搜索

解析:状态d[i][j]代表r=i , c=j这个位置能滑的最大长度.深搜+备忘录 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int maxn=100+5; int R,C; int a[maxn][maxn]; int d[maxn][maxn]; int dr[]={0,-1,0,1};

poj 1088 滑雪(区间dp+记忆化搜索)

题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程:由于由位置[i, j]只能向四个方向移动,所以子问题最多有四个:所以dp[i][j]为其邻域可以滑雪的最大区域长度加上从该位置滑到邻域的长度,即1: 代码如下: #include <cstdio> #include <iostream> #include <algorithm&