Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 74996 | Accepted: 27818 |
Description
Michael喜欢滑雪百这并不奇怪, 由于滑雪的确非常刺激。但是为了获得速度,滑的区域必须向下倾斜,并且当你滑到坡底,你不得不再次走上坡或者等待升降机来载你。Michael想知道载一个区域中最长底滑坡。区域由一个二维数组给出。数组的每一个数字代表点的高度。以下是一个样例
一个人能够从某个点滑向上下左右相邻四个点之中的一个,当且仅当高度减小。在上面的样例中。一条可滑行的滑坡为24-17-16-1。当然25-24-23-...-3-2-1更长。其实,这是最长的一条。
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
输入的第一行表示区域的行数R和列数C(1 <= R,C <= 100)。以下是R行,每行有C个整数,代表高度h。0<=h<=10000。
Output
输出最长区域的长度。
Sample Input
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
中文题什么的再也不用操心题目都看不懂了。
。23333
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
const int M = 105;
int n, m;
int map[M][M];
int ans[M][M];
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
int dp(int x, int y)
{
int max = 0;
if( ans[x][y]>0 )
return ans[x][y];
for(int i=0; i<4; i++) //四个方向
{
int xx = x + dx[i];
int yy = y + dy[i];
if( xx>=1 &&xx<=n &&yy>=1 &&yy<=m ) //边界
{
if( map[x][y] > map[xx][yy] ) //从高到低才合法
if ( max < dp( xx, yy ) )
max = dp( xx, yy );
}
}
return ans[x][y] = max + 1;
}
int main()
{
while( scanf( "%d%d", &n, &m ) !=EOF )
{
memset( map, 0, sizeof(map) );
memset( ans, 0, sizeof(ans) );
for( int i=1; i<=n; i++ )
for( int j=1; j<=m; j++ )
scanf( "%d", &map[i][j] );
for( int i=1; i<=n; i++ )
for( int j=1; j<=m; j++ )
dp( i, j );
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
if( ans[1][1] < ans[i][j] )
ans[1][1] = ans[i][j];
printf("%d\n", ans[1][1]);
}
return 0;
}