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

重做了一遍这到n年前做过的题,原先的代码是这样的
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 111
 4 #define MAX(x,y) x>y?x:y
 5 int huaxue[111][111];
 6 int len[111][111];
 7
 8 int getmax(int i,int j,int R,int C,int huaxue[][111],int len[][111])
 9 {
10       int temp=1;
11
12         if (i-1>=0&&huaxue[i-1][j]<huaxue[i][j])
13          temp=1+(len[i][j]==0?getmax(i-1,j,R,C,huaxue,len):len[i-1][j]);
14         if (i+1<=R-1&&huaxue[i+1][j]<huaxue[i][j])
15          temp=(MAX(temp,1+(len[i][j]==0?getmax(i+1,j,R,C,huaxue,len):len[i+1][j])  ) );
16         if (j-1>=0&&huaxue[i][j-1]<huaxue[i][j])
17          temp=(MAX(temp,1+(len[i][j]==0?getmax(i,j-1,R,C,huaxue,len):len[i][j-1])  ) );
18         if (j+1<=C-1&&huaxue[i][j+1]<huaxue[i][j])
19          temp=(MAX(temp,1+(len[i][j]==0?getmax(i,j+1,R,C,huaxue,len):len[i][j+1])  ) ) ;
20
21         len[i][j]=temp;
22
23     return temp;
24 }
25 main()
26 {
27     int R,C,i,j,result;
28     //freopen("input.txt","r",stdin);
29     scanf("%d %d",&R,&C);
30
31     for(i=0;i<R;i++)
32     {
33         for(j=0;j<C;j++)
34         scanf("%d",&huaxue[i][j]);
35         len[i][j]=0;
36     }
37      result=0;
38
39     for(i=0;i<R;i++)
40       for(j=0;j<C;j++)
41
42             result=(MAX(result,getmax(i,j,R,C,huaxue,len)));
43
44
45     printf("%d\n",result);
46     return 0;
47 }

这次提交的代码是这样的

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4
 5 int height[102][102];
 6 int cnt[102][102];
 7 int dir[][4] = {{0,1},{0,-1},{1,0},{-1,0}};
 8 int r,c;
 9
10 int dfs(int x, int y) {
11     if(cnt[x][y] != 0) {
12         return cnt[x][y];
13     }
14     int tmpMax = 1;
15     for(int i = 0; i < 4; i++) {
16         int tmpx = x + dir[i][0];
17         int tmpy = y + dir[i][1];
18         int tmpC = 0;
19         if(tmpx >= 0 && tmpy >= 0 && tmpx < r && tmpy < c && height[tmpx][tmpy] < height[x][y]) {
20             tmpC = 1 + dfs(tmpx, tmpy);
21         }
22         if(tmpMax < tmpC) {
23             tmpMax = tmpC;
24         }
25     }
26     cnt[x][y] = tmpMax;
27     return tmpMax;
28 }
29
30 int main(int argc, char const *argv[])
31 {
32     //freopen("input.txt","r",stdin);
33     while(scanf("%d %d",&r,&c) != EOF) {
34         memset(cnt,0,sizeof(cnt));
35         for(int i = 0; i < r; i++) {
36             for(int j = 0; j < c; j++) {
37                 scanf("%d",&height[i][j]);
38             }
39         }
40         int max = 1;
41         for(int i = 0; i < r; i++) {
42             for(int j = 0; j < c; j++) {
43                 int tmp = dfs(i,j);
44                 if(max < tmp) {
45                     max = tmp;
46                 }
47             }
48         }
49         printf("%d\n",max);
50     }
51     return 0;
52 }

第一次提交居然没通过,因为第18行tmpC没有初始化!

时间: 2024-08-02 22:06:39

poj 1088滑雪的相关文章

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

滑雪 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 滑雪(记忆化搜索)

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

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&

POJ 1088 滑雪 (深搜)

题目链接:http://poj.org/problem?id=1088 题面: 滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 83760   Accepted: 31349 Description Michael喜欢滑雪这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道载一个区域中最长底滑坡.区域由一个二维数组给出

poj 1088 滑雪(线性DP)

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

poj 1088 滑雪 DP(dfs的记忆化搜索)

题目地址:http://poj.org/problem?id=1088 题目大意:给你一个m*n的矩阵 如果其中一个点高于另一个点 那么就可以从高点向下滑 直到没有可以下滑的时候 就得到一条下滑路径 求最大的下滑路径 分析:因为只能从高峰滑到低峰,无后效性,所以每个点都可以找到自己的最长下滑距离(只与自己高度有关).记忆每个点的最长下滑距离,当有另一个点的下滑路径遇到这个点的时候,直接加上这个点的最长下滑距离. dp递推式是,dp[x][y] = max(dp[x][y],dp[x+1][y]+