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 |
个人的简单思路,分享下,代码已AC#include<stdio.h> #define R 101 #define C 101 int h=0; int map[R+1][C+1]; int high[R+1][C+1]; int r,c; int fac(int i,int j) { int temp=0,max_high=0;//max_high为当前坐标的四个方向的最大降序长度 if(i<r&&j<c&&i>-1&&j>-1)//越界判断 { if(high[i+1][j]==1&&map[i+1][j]<map[i][j])//若下一方向的记忆数组的值为初始值且满足降序,则向下探索 temp=fac(i+1,j); else if(map[i+1][j]<map[i][j])//否则的话如果是降序但记忆数组的值不是初始值,则说明下一方向此前已探索,将其最大长度记录给temp temp=high[i+1][j]; if(temp>max_high)//替换较大的降序长度 max_high=temp; if(high[i][j+1]==1&&map[i][j+1]<map[i][j]) temp=fac(i,j+1); else if(map[i][j+1]<map[i][j]) temp=high[i][j+1]; if(temp>max_high) max_high=temp; if(high[i-1][j]==1&&map[i-1][j]<map[i][j]) temp=fac(i-1,j); else if(map[i-1][j]<map[i][j]) temp=high[i-1][j]; if(temp>max_high) max_high=temp; if(high[i][j-1]==1&&map[i][j-1]<map[i][j]) temp=fac(i,j-1); else if(map[i][j-1]<map[i][j]) temp=high[i][j-1]; if(temp>max_high) max_high=temp; return max_high+1; } else return 0;//数组越界 } /* 思路为将大问题分割为等价小问题,即将全局最优分解为局部最优,为了避免多余的计算,设一辅助记忆数组high[][]记忆每个坐标 的最大降序长度,用递归实现 */ int main() { int i,j; scanf("%d%d",&r,&c); for(i=0;i<r;i++) for(j=0;j<c;j++) scanf("%d",&map[i][j]); for(i=0;i<r;i++)//辅助数组初始化 for(j=0;j<c;j++) high[i][j]=1; for(i=0;i<r;i++)//逐一计算每个位置的最大降序长度 for(j=0;j<c;j++) high[i][j]=fac(i,j); for(i=0;i<r;i++) for(j=0;j<c;j++) if(high[i][j]>h) h=high[i][j]; printf("%d\n",h); } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator