poj 1088 滑雪 (dp)
题意就不多解释了,题目思路很简单,搜索就行了,但是暴搜会TLE,但是对于每一个dfs(X, Y),如果参数一样的话结果也是一样的,我们用一个二维数组去保存每次计算的位置的结果,最后从所有的解里面挑出一个最大值即可。也许一开始考虑的时候会有这样的想法,是不是从最高的位置开始找就能找到解,或者从最低的位置开始,起始不是这样的,所以我们应该去搜索每一个位置为起点的时候的解。
我还因为一点小问题RE哭了,POJ不支持以下语法,阔以原谅,毕竟OJ很早了
return dp[x][y] = 1;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 110;
int r, c, h[maxn][maxn], dp[maxn][maxn];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
namespace ans{
const int inf = 0x3f3f3f3f;
int dfs(int x, int y){
if(dp[x][y] >= 0)return dp[x][y];
int res = 0;
int f = 0;
for (int i = 0; i < 4; i++){
int xx = x + dx[i];
int yy = y + dy[i];
if(xx >= 1 && xx <= r && yy >= 1 && yy <= c && h[xx][yy] < h[x][y]){
res=max(res, dfs(xx, yy) + 1);
f++;
}
}
if(!f) {
dp[x][y] = 1;
return 1;
}
dp[x][y] = res;
return res;
}
void MAIN(){
scanf("%d %d", &r, &c);
for (int i = 1; i <= r; i++){
for (int j = 1; j <= c; j++){
scanf("%d", &h[i][j]);
}
}
int res = 0;
memset(dp, -1, sizeof dp);
for (int i = 1; i <= r; i++){
for (int j = 1; j <= c; j++){
int aans = dfs(i, j);
res = max(aans, res);
}
}
printf("%d\n", res);
}
}
int main()
{
ans::MAIN();
return 0;
}
本文来自博客园,作者:correct,转载请注明原文链接:https://www.cnblogs.com/correct/p/12862086.html

浙公网安备 33010602011771号