| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Re:不好意思,不小心贴了其他题的代码,现去掉。。。In Reply To:我的改进版,0MS~~ Posted by:majuncheng at 2009-08-23 16:45:54 >
> >
> #include <cstdio>
>
> int height[102][102]; //高度
> int wt[102][102]; //记录各点到达的最长长度
> int r,c; //行数和列数
>
> int DP(int i,int j)
> {
> if (wt[i][j])
> return wt[i][j]; //如果已经递归过,长度值就不为初始值(0)了,避免重复运算
> int max=0;
> int p,h=height[i][j]; //利用递归找到邻近点中长度最长的
> if ((p=i-1)>=0 && height[p][j]<h)
> if ( DP(p,j)>max)
> max=DP(p,j);
> if ((p=i+1)<r && height[p][j]<h)
> if (DP(p,j)>max)
> max=DP(p,j);
> if ((p=j-1)>=0 && height[i][p]<h)
> if (DP(i,p)>max)
> max=DP(i,p);
> if ((p=j+1)<c && height[i][p]<h)
> if (DP(i,p)>max)
> max=DP(i,p);
> wt[i][j]=max+1; //这里使用了和上面楼主一样的技巧,如果四个更新后max仍为0,会被赋以初始值
> return wt[i][j];
> }
>
> int main()
> {
> scanf ("%d%d",&r,&c);
> int max=0;
> for (int i=0;i<r;++i)
> for (int j=0;j<c;++j)
> {
> scanf("%d",&height[i][j]);
> wt[i][j]=0;
> }
> for (int i=0;i<r;++i)
> for (int j=0;j<c;++j)
> if (DP(i,j)>max)
> max=DP(i,j);
> printf("%d\n",max);
>
> return 0;
> }
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator