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:直接记忆化搜索,O(n)In Reply To:Re:直接记忆化搜索,O(n) Posted by:bigbig85 at 2007-06-04 22:25:40 #include<iostream.h> int r,c,a[101][101],flag[101][101]; int huaxue(int i,int j) { int temp,max=1; if(flag[i][j]!=-1) return flag[i][j]; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 这一步保证每个状态只算一次,也就是所谓的“记忆” 时间复杂度是O(n^2),我刚说错了 if(a[i][j]>a[i-1][j]&&i>1) { temp=huaxue(i-1,j); if(max<temp+1) max=temp+1; } if(a[i][j]>a[i][j-1]&&j>1) { temp=huaxue(i,j-1); if(max<temp+1) max=temp+1; } if(a[i][j]>a[i+1][j]&&i<r) { temp=huaxue(i+1,j); if(max<temp+1) max=temp+1; } if(a[i][j]>a[i][j+1]&&j<c) { temp=huaxue(i,j+1); if(max<temp+1) max=temp+1; } flag[i][j]=max; return max; } void main() { int i,j,temp,max=0; cin>>r>>c; for(i=1;i<=r;i++) for(j=1;j<=c;j++) {cin>>a[i][j]; flag[i][j]=-1; } for(i=1;i<=r;i++) for(j=1;j<=c;j++) {temp=huaxue(i,j); if(max<temp) max=temp; } cout<<max<<endl; } > 就是直接递归么?看了一下有人贴的一个OMS的算法,不是很知道哪里用了记忆。 > 没写过记忆化搜索的程序,能大概说一下怎么样记忆化搜索么? Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator