skiing

问题 B: 【动态规划】skiing

时间限制: 1 Sec   内存限制: 128 MB
提交: 157   解决: 83
[ 提交][ 状态][ 讨论版][命题人: 外部导入]

题目描述

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更长。事实上,这是最长的一条。

输入

第一行表示有几组测试数据,输入的第二行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。
后面是下一组数据;

输出

输出最长区域的长度。

样例输入

1
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

样例输出

25

提示

分析:一道记忆化搜索题,记忆化搜索简单来说好像就是dp+dfs,每次dfs都记录一下值。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}

ll n,m,ans;
ll a[105][105];
ll dp[105][105]={0};
ll dir[4][2]={1,0,-1,0,0,1,0,-1};
ll F(ll x,ll y)
{
    if(dp[x][y]>0)
        return dp[x][y];
    dp[x][y]=1;
    for(ll i=0;i<4;i++)
    {
        ll fx=dir[i][0]+x;
        ll fy=dir[i][1]+y;
        if(fx>=1 && fy>=1 && fx<=n && fy<=m)
        {
            if(a[x][y]<a[fx][fy])
            {
                dp[x][y]=max(dp[x][y],F(fx,fy)+1);
            }
        }
    }
    ans=max(ans,dp[x][y]);
    return dp[x][y];
}

int main()
{
    ll i,j,t;

    scanf("%lld",&t);
    while(t--)
    {
        memset(dp,0,sizeof(dp));
        ans=1;

        scanf("%lld%lld",&n,&m);
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                scanf("%lld",&a[i][j]);

        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++)
                F(i,j);

        printf("%lld\n",ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值