本题使用的是动态规划算法~先把每个点按从小到大排序,然后扫描每个点s,对于s点周围的4个点,如果其高度大于s,且其长度比s的长度+1小,则其长度为s点的长度+1.搞定. #include<iostream> #include<cstdlib> using namespace std; const int MAX_H = 10000; const int MAX_RC = 102; struct Point { int x; int y; int h; }; int cmp( const void *a ,const void *b)//快速排序的参考函数 { return (*(Point *)a).h > (*(Point *)b).h ? 1 : -1; } Point point[MAX_H];//存储每个点的横座标,纵座标,高度 int Height[MAX_RC][MAX_RC];//存储高度的距阵 int Length[MAX_RC][MAX_RC];//存储以此为起点的区域的长度 void main() { int C,R,i,j,k; cin>>R>>C; k=0; for(i=1;i<=R;i++)//读入距阵 for(j=1;j<=C;j++) { cin>>Height[i][j]; point[k].x=i; point[k].y=j; point[k].h=Height[i][j]; k++; } for(i=0;i<=R+1;i++)for(j=0;j<=C+1;j++)Length[i][j]=0;//初始化各点的区域长度为零 qsort(point,R*C,sizeof(point[0]),cmp);//将每个点按高度进行排序 for(i=0;i<C*R;i++)//扫描每个点 { if((point[i].h<Height[point[i].x-1][point[i].y])&&(Length[point[i].x-1][point[i].y]<Length[point[i].x][point[i].y]+1)) Length[point[i].x-1][point[i].y]=Length[point[i].x][point[i].y]+1; if((point[i].h<Height[point[i].x+1][point[i].y])&&(Length[point[i].x+1][point[i].y]<Length[point[i].x][point[i].y]+1)) Length[point[i].x+1][point[i].y]=Length[point[i].x][point[i].y]+1; if((point[i].h<Height[point[i].x][point[i].y-1])&&(Length[point[i].x][point[i].y-1]<Length[point[i].x][point[i].y]+1)) Length[point[i].x][point[i].y-1]=Length[point[i].x][point[i].y]+1; if((point[i].h<Height[point[i].x][point[i].y+1])&&(Length[point[i].x][point[i].y+1]<Length[point[i].x][point[i].y]+1)) Length[point[i].x][point[i].y+1]=Length[point[i].x][point[i].y]+1; } int Answer=0;//输出结果 for(i=1;i<=R;i++) for(j=1;j<=C;j++) if(Answer<Length[i][j])Answer=Length[i][j]; cout<<Answer+1<<endl; }