COJN 0575 800601滑雪

 

 

800601滑雪
难度级别:B; 运行时间限制:1000ms; 运行空间限制:51200KB; 代码长度限制:2000000B
试题描述

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更长。事实上,这是最长的一条。

 

输入
输入的第一行表示区域的行数R和列数C,下面是R行,每行有C个整数,代表高度h。
输出
输出最长区域的长度。
输入示例
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
输出示例
25
其他说明
数据范围:1<= R,C<=100,0<=h<=10000.

题解:一眼dp。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(' ')
 8 #define ENT putchar('\n')
 9 using namespace std;
10 const int maxn=100+10,inf=1e8;
11 inline void write(int x);
12 int f[maxn][maxn],n,m,A[maxn][maxn];
13 int mx[]={0,0,-1,1},my[]={-1,1,0,0};
14 int dp(int x,int y){
15     if(f[x][y]>=0)return f[x][y];f[x][y]=0;
16     for(int d=0;d<4;d++){
17         int tx=mx[d]+x,ty=my[d]+y;
18         if(tx>=0&&ty>=0&&tx<n&&ty<m&&A[tx][ty]<A[x][y]){
19             f[x][y]=max(f[x][y],dp(tx,ty)+1);
20         }
21     }
22     return f[x][y];
23 }
24 inline int read(){
25     int x=0,sig=1;char ch=getchar();
26     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
27     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
28     return sig?x:-x;
29 }
30 inline void write(int x){
31     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
32     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
33     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
34 }
35 void init(){
36     memset(f,-1,sizeof(f));
37     n=read();m=read();
38     for(int i=0;i<n;i++)
39         for(int j=0;j<m;j++)
40             A[i][j]=read();
41     int ans=-1;
42     for(int i=0;i<n;i++)
43         for(int j=0;j<n;j++)
44             ans=max(ans,dp(i,j));
45     write(ans+1);
46     return;
47 }
48 void work(){
49     return;
50 }
51 void print(){
52     return;
53 }
54 int main(){init();work();print();return 0;}

 

posted @ 2015-07-24 10:31  AI_Believer  阅读(236)  评论(0编辑  收藏  举报