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 |
哪位高手帮俺看一下======一道难倒n人的小题,此代码,运行样例正确,提交时老是timeerror #include"iostream.h" int dx[6]={1,-1,0, 0,0, 0}; int dy[6]={0, 0,1,-1,0, 0}; int dz[6]={0, 0,0, 0,1,-1}; short a[12][12][12]={0};//全局,注意赋值法,同时在其外多添上一层 typedef struct{ short x; short y; short z; }elemtype; elemtype Stack[1100] ; int h,t,max,min; int re(int p) { if(p>max) p--; if(p<0) p++; return p; } int mgpath() { int find,top,di=0;; int count=0; for(int i=0;max>=i;i++) for(int j=0;max>=j;j++) for(int k=0;max>=k;k++) if(a[i][j][k]==0) { top=0; Stack[top].x=i;Stack[top].y=j;Stack[top].z=k; a[i][j][k]=1; while (top>-1) /*栈不空时循环,每次只处理一个点*/ { find=0; /*找下一个可走方块*/ di=-1; while (di<6 && find==0)//di会接着上次加 { di++; switch(di) { case 0:i=i+1;break; case 1:j=j+1;break; case 2:k=k+1;break; case 3:i=i-1;break; case 4:j=j-1;break; case 5:k=k-1;break; } if (a[i][j][k]==0&&max>=i&&i>=0&&max>=j&&j>=0&&max>=k&&k>=0) find=1; else { switch(di) { case 0:i=i-1;break; case 1:j=j-1;break; case 2:k=k-1;break; case 3:i=i+1;break; case 4:j=j+1;break; case 5:k=k+1;break; } } } if (find==1) /*找到了下一个可走方块*/ { top++; /*下一个可走方块进栈*/ Stack[top].x=i;Stack[top].y=j;Stack[top].z=k; a[i][j][k]=1; /*避免重复走到该方块*/ } else /*没有路径可走,则退栈*/ { a[Stack[top].x][Stack[top].y][Stack[top].z]=1; top--; } } count++; } return count; } void main() { int n,c,i,j,k,x,y,z,count,u=0; cin>>c; int cc=c; int b[20]; while(cc>0) { max=0; min=200; cin>>n; t=0; for(count=1;count<=n;count++){ cin>>x>>y>>z; a[x][y][z]=1; if(x>max) max=x; if(x<min) min=x; if(y>max) max=y; if(y<min) min=y; if(z>max) max=z; if(z<min) min=z; } max++; min--; b[u++]=mgpath()-1; for(i=0;i<=max;i++){ for(j=0;j<=max;j++){ for(k=0;k<=max;k++){ a[i][j][k]=0; } } } cc--; } for(u=0;u<c;u++) cout<<b[u]<<endl; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator