首页 > 代码库 > POJ 1088 滑雪 记忆化搜索

POJ 1088 滑雪 记忆化搜索

解析:状态d[i][j]代表r=i , c=j这个位置能滑的最大长度。深搜+备忘录

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn=100+5;int R,C;int a[maxn][maxn];int d[maxn][maxn];int dr[]={0,-1,0,1};int dc[]={-1,0,1,0};int solve(int r,int c){    if(d[r][c]>0) return d[r][c];    int next_r,next_c;    int maxv=0;    for(int i=0;i<4;i++)    {        next_r=r+dr[i];        next_c=c+dc[i];        if(next_r<=0 || next_r>R || next_c<=0 || next_c>C)            continue;        if(a[r][c]>a[next_r][next_c])            maxv=max(maxv,solve(next_r,next_c));    }    return d[r][c]=maxv+1;}int main(){    //freopen("in.txt","r",stdin);    while(cin>>R>>C)    {        for(int i=1; i<=R; i++)            for(int j=1; j<=C; j++)                scanf("%d",&a[i][j]);        memset(d,0,sizeof(d));        int ans=0;        for(int i=1;i<=R;i++)            for(int j=1;j<=C;j++)                ans=max(ans,solve(i,j));        printf("%d\n",ans);    }    return 0;}

 

POJ 1088 滑雪 记忆化搜索