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 |
大牛们来看看,我的前两个程序为什么错了?调了一下午,求助help~~~~~~~~~~~//DP,把高度排序后,从小到大算,每次都计算周围四个中最大的那个,是从bellman算法想 //到的,但不知道为什么错。我分别用hash和sort排序都过不了~~~ //关键是测试了近百个变态的测试数据都没有错误(递归DP的算法ac以后用来对比校验) //第一个:sort排序 #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int M,N; int hi[1000][1000]; int st[1000][1000]; struct nod { int x,y,h; }data[11000000]; bool cmp(nod a,nod b) { return a.h<b.h; } int main() { int top; int i,j,k,t,max_t; freopen("1.txt","r",stdin); while (scanf("%d%d",&M,&N)!=EOF) { memset(st,0,sizeof(st)); for(i=0;i<=M+1;++i) for(j=0;j<=N+1;++j) hi[i][j]=-1; top=0; for (i=1;i<=M;++i) { for (j=1;j<=N;++j) { scanf("%d",&t); data[top].x=i,data[top].y=j,data[top].h=t; top++; hi[i][j]=t; } } sort(data,data+top,cmp); for (k=0;k<top;++k) { i=data[k].x,j=data[k].y; max_t=st[i][j]; if (hi[i+1][j]<hi[i][j]&&st[i+1][j]>=max_t) max_t=st[i+1][j]+1; if (hi[i-1][j]<hi[i][j]&&st[i-1][j]>=max_t) max_t=st[i-1][j]+1; if (hi[i][j+1]<hi[i][j]&&st[i][j+1]>=max_t) max_t=st[i][j+1]+1; if (hi[i][j-1]<hi[i][j]&&st[i][j-1]>=max_t) max_t=st[i][j-1]+1; st[i][j]=max_t; } max_t=1; for(i=1;i<=M;++i) for(j=1;j<=N;++j) { if(st[i][j]>max_t) max_t=st[i][j]; } printf("%d\n",max_t); } return 0; } //第二个程序,用hash拉链排序~ /*struct nod { int x,y; int h; nod* next; }Pool[110000],*hash[100100000],*p,*ma; int main() { int i,j,k,max_t; freopen("1.txt","r",stdin); while (scanf("%d%d",&M,&N)!=EOF) { memset(st,0,sizeof(st)); memset(hash,0,sizeof(hash)); memset(hi,0xff,sizeof(hi)); ma=Pool; for (i=1;i<=M;++i) { for (j=1;j<=N;++j) { scanf("%d",&hi[i][j]); ma->x=i,ma->y=j; ma->next=hash[hi[i][j]]; hash[hi[i][j]]=ma; ma++; } } for(k=0;k<=100000000;++k) { if(hash[k]) { p=hash[k]; while(p!=NULL) { i=p->x,j=p->y; max_t=st[i][j]; if (hi[i+1][j]<hi[i][j]&&st[i+1][j]>=max_t) max_t=st[i+1][j]+1; if (hi[i-1][j]<hi[i][j]&&st[i-1][j]>=max_t) max_t=st[i-1][j]+1; if (hi[i][j+1]<hi[i][j]&&st[i][j+1]>=max_t) max_t=st[i][j+1]+1; if (hi[i][j-1]<hi[i][j]&&st[i][j-1]>=max_t) max_t=st[i][j-1]+1; st[i][j]=max_t; p=p->next; } } } max_t=1; for(i=1;i<=M;++i) for(j=1;j<=N;++j) { if(st[i][j]>max_t) max_t=st[i][j]; } printf("%d\n",max_t); } return 0; }*/ //谢谢 Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator