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 |
超时,请大牛们给点意见/////////////////////////////////////动态规划初次尝试 ////////////////////////////////////// #include <iostream> #include <stdlib.h> #include <list> #include <vector> using namespace std; class rectangle//存储矩形信息的类 { public: int m,n,x,y,sum; bool isMaxitself; list<rectangle>::iterator maxsub; rectangle(int mt,int nt,int xt,int yt) {m=mt;n=nt;x=xt;y=yt;sum=0;maxsub=NULL;isMaxitself=false;} bool operator==(const rectangle obj) { if(obj.x==x&&obj.y==y&&obj.m==m&&obj.n==n) return true; return false; } }; list<rectangle>::iterator f(int,int,int,int); list<rectangle> ls;//存储各个矩形的链表 vector<int> rec; //存储数阵的向量 int main(int argc, char *argv[]) { int N,t; cin>>N; rec.push_back(N); for(int i=0;i<N*N;i++) {cin>>t;rec.push_back(t);} list<rectangle>::iterator s; s=f(N,N,1,1); cout<<s->maxsub->sum<<endl; //cout<<"ls :"<<endl; //for(s=ls.begin();s!=ls.end();s++) cout<<"m:"<<s->m<<" n: "<<s->n<<" x:"<<s->x<<" y:"<<s->y<<" maxsub:"<<s->maxsub->sum<<endl; //system("PAUSE"); return 0; } list<rectangle>::iterator f(int m,int n,int x,int y) { int i,j; //?重复搜索(测试输出的错误,其实避免重复搜索功能已经生效了),?最大子矩阵计算 (解决) if(m==0||n==0) return NULL; list<rectangle>::iterator result=find(ls.begin(),ls.end(),rectangle(m,n,x,y)); if(result!=ls.end()) return result;//该矩形已经找过 rectangle q(m,n,x,y); int sumt=0; for(i=0;i<m;i++) for(j=0;j<n;j++) sumt+=rec[rec[0]*(y+i-1)+x+j]; //求出本矩形的元素值总合 //把自己的信息加入链表 行列和坐标的关系要搞清楚 ls.push_front(q); list<rectangle>::iterator thisit=ls.begin(); thisit->sum=sumt; list<rectangle>::iterator sub[4],p=NULL; //分别划掉上下左右 sub[0]=f(m,n-1,x,y);sub[1]=f(m,n-1,x+1,y);sub[2]=f(m-1,n,x,y);sub[3]=f(m-1,n,x,y+1); //取出最大的 for(i=0;i<4;i++) { if(sub[i]!=NULL&&p==NULL) {p=sub[i];continue;} if(sub[i]!=NULL&&p!=NULL) if((p->maxsub->sum)<(sub[i]->maxsub->sum)) p= sub[i]; } //若本矩形只有一点 if(p==NULL) {thisit->isMaxitself=true;thisit->maxsub=thisit;return thisit;} //正常比较 if(sumt>(p->maxsub->sum)) {thisit->isMaxitself=true;thisit->maxsub=thisit;return thisit;} else{thisit->isMaxitself=false;thisit->maxsub=p->maxsub;return thisit;} //疏忽 把p->maxsub写成了p } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator