uva 10285——Longest Run on a Snowboard

题意:在一个R*c的矩阵上找一条高度严格递减的最长路,起点任意,每次可以走上下左右。


思路:DAG上的最长路问题,直接套用记忆化搜索的模板,dp(i,j)=max(dp(ii,jj)四个方向最大值),然后找出来最大的即可。


code:

#include <bits/stdc++.h>
using namespace std;

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int N=1000000;
const int M=105;
const int mod=1000000007;
const double pi=acos(-1.0);

#define cls(x,c) memset(x,c,sizeof(x))
#define ft(i,s,t) for (int i=s;i<=t;i++)

int w,h;
int mp[M][M],dp[M][M];
char s[M];
int tx[]={1,-1,0,0};
int ty[]={0,0,1,-1};
bool ok(int i,int j)
{
    return (i>=0&&i<w&&j>=0&&j<h);
}
int sol(int i,int j)
{
    int f=0;
    int& ans=dp[i][j];
    if (ans!=-1) return ans;
    ans=-1;
    for (int p=0;p<4;p++)
    {
        int x=i+tx[p],y=j+ty[p];
        if (mp[x][y]<mp[i][j]&&ok(x,y))
        {
            ans=max(ans,sol(x,y)+1);f=1;
        }
    }
    if (!f) ans=1;
    return ans;
}
int main()
{
    int T;
    scanf("%d",&T);
    while (T--)
    {
        scanf("%s %d %d",s,&w,&h);
        ft(i,0,w-1) ft(j,0,h-1) scanf("%d",&mp[i][j]);
        cls(dp,-1); int ans=-1;
        ft(i,0,w-1) ft(j,0,h-1) ans=max(ans,sol(i,j));
        printf("%s: %d\n",s,ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值