首页 > 代码库 > UVA10285 Longest Run on a Snowboard

UVA10285 Longest Run on a Snowboard

题解:

记忆化搜索入门题

代码:

#include<bits/stdc++.h>using namespace std;#define pb push_back#define mp make_pair#define se second#define fs first#define LL long long#define CLR(x) memset(x,0,sizeof x)#define MC(x,y) memcpy(x,y,sizeof(x))  #define SZ(x) ((int)(x).size())#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1 typedef pair<int,int> P;const double eps=1e-9;const int maxn=200100;const int mod=1e9+7;const int INF=1e9;int M[110][110];int dp[110][110];char name[50];int r,c;int dx[4]={1,-1,0,0};int dy[4]={0,0,1,-1};int DP(int x,int y){    int &m=dp[x][y];    if(m) return m;    m=1;    for(int i=0;i<4;i++)    {        int a=x+dx[i];        int b=y+dy[i];        if(a>0&&a<=r&&b>0&&b<=c&&M[a][b]<M[x][y]) m=max(DP(a,b)+1,m);    }    return m;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        memset(dp,0,sizeof(dp));        scanf("%s %d %d",name,&r,&c);        for(int i=1;i<=r;i++)        for(int j=1;j<=c;j++) scanf("%d",&M[i][j]);        int Max=1;        for(int i=1;i<=r;i++)        for(int j=1;j<=c;j++) Max=max(Max,DP(i,j));        printf("%s: %d\n",name,Max);    }    return 0;}

 

UVA10285 Longest Run on a Snowboard