BNUOJ 23905 滑雪
阅读原文时间:2023年07月12日阅读:1

Time Limit: 1000ms

Memory Limit: 65536KB

This problem will be judged on UESTC. Original ID: 1309
64-bit integer IO format: %lld      Java class name: Main

Michael喜欢滑雪, 因为滑雪的确很刺激。可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道在一个区域中最长的滑坡。区域由一个二维数组给出。数组的每个数字代表点的高度。下面是一个例子
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
一个人可以从某个点滑向上下左右相邻四个点之一,当且仅当高度减小。在上面的例子中,一条可滑行的滑坡为24-17-16-1。当然25-24-23-…-3-2-1更长。事实上,这是最长的一条。

Input

第一行是一个整数t,代表case数。

对于每一个case:
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。下面是R行,每行有C个整数,代表高度h,0<=h<=10000。

Output

输出最长区域的长度。

Sample Input

1
5 5
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9

Sample Output

25

Source

SHTSC 2002

解题:我以前总是担心这样搜索,会不会搜回来,其实这个是按从高到低的,严格从高到底,所有不会搜回来的!

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int mp[][],r,c,dp[][];
int dfs(int x,int y,int h) {
static int dir[][] = {,-,,,-,,,};
if(x < || x >= r || y < || y >= c || mp[x][y] >= h) return ;
if(dp[x][y]) return dp[x][y];
int theMax = ;
for(int i = ; i < ; i++){ int tx = x+dir[i][]; int ty = y+dir[i][]; int temp = dfs(tx,ty,mp[x][y])+; if(temp > theMax) theMax = temp;
}
return dp[x][y] = theMax;
}
int main() {
int ans = ;
scanf("%d %d",&r,&c);
for(int i = ; i < r; i++){
for(int j = ; j < c; j++){
scanf("%d",mp[i]+j);
dp[i][j] = ;
}
}
for(int i = ; i < r; i++){
for(int j = ; j < c; j++){
ans = max(ans,dfs(i,j,INF));
}
}
cout<<ans<<endl;
return ;
}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章