POJ1088 滑雪 | UVa10285 - Longest Run on a Snowboard

本文详细解析了POJ1088滑雪问题的解决方案,采用记忆化搜索(mDFS)算法来求解最长路径。通过递归地探索所有可能的下坡路径,并利用动态规划避免重复计算,最终找出从任意起点开始的最大滑行长度。


POJ1088 滑雪:http://poj.org/problem?id=1088


Tags: mDFS(记忆化搜索)

Code:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int maxn = 110;

int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
int h[maxn][maxn];
int dp[maxn][maxn];
int R, C;
int DP(int x, int y)
{
     int i, xx, yy;
     if(dp[x][y]!=-1) return dp[x][y];
     int ans = 0;
     for(i=0; i<4; i++) {
          xx = x+dx[i];
          yy = y+dy[i];
          if(xx<0||xx>=R||yy<0||yy>=C||h[x][y]<=h[xx][yy]) continue;
          ans = max(ans, DP(xx,yy) );
     }
     return dp[x][y] = ans + 1;
}
int main()
{
     int i, j, ans, tmp;
     while(cin>>R>>C) {
          ans = 0;
          for(i=0; i<R; i++)
               for(j=0; j<C; j++) {
                    cin>>h[i][j];
               }
          memset(dp,-1,sizeof(dp));
          for(i=0; i<R; i++)
               for(j=0; j<C; j++) {
                    tmp = DP(i,j);
                    ans = max(ans,tmp);
               }
          cout<<ans<<endl;
     }
     return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值