POJ - 1088 滑雪

19 篇文章 0 订阅

题目地址:https://vjudge.net/problem/16378/origin

题目:

It's not surprising that Wanggann likes skiing, because skiing is really exciting. However, in order to gain speed, the sliding area must be tilted downwards, and when you slide to the bottom of the hill, you have to go uphill again or wait for the lift to load you.Wanggann wants to know the longest bottom landslide in one area. The area is given by a two-dimensional array. Each number in the array represents the height of the point. For example: 

 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


A slider can slide from one point up and down to one of the four adjacent points iff the height decreases. In the above example, a glideable landslide is 24-17-16-1. Of course 25-24-23-...-3-2-1 is longer. In fact, this is the longest one.

Input

The first line entered indicates the number of rows R and the number of columns C (1 <= R, C <= 100). The following is an R row with C integers for each row, representing the height h, 0 <= h <= 10000.

Output

The length of the longest area.

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

 

思路:记忆化搜索,也就是dfs+dp。

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
int maze[101][101];
int m,n;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};
int dp[101][101];
int dfs(int x,int y)
{
    for(int i=0;i<4;i++)
    {
        int nx=x+dx[i],ny=y+dy[i];
        if(nx>=0&&nx<m&&ny>=0&&ny<n&&maze[nx][ny]>maze[x][y]){
                if(dp[nx][ny])dp[x][y]=max(dp[x][y],1+dp[nx][ny]);
                else dp[x][y]=max(dp[x][y],1+dfs(nx,ny));
        }
    }
    return dp[x][y];
}
int main()
{
    while(cin>>m>>n){
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                scanf("%d",&maze[i][j]);
            }
        }
        memset(dp,0,sizeof(dp));
        int mmax=0;
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(dp[i][j]==0)dfs(i,j);
                mmax=max(mmax,dp[i][j]);
            }
        }
        cout<<mmax+1<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值