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:AC了~发贴庆贺~!~~~In Reply To:AC了~发贴庆贺~!~~~ Posted by:Ikki at 2005-08-30 16:49:59 能帮忙看下这个程序哪里有错吗? #include<stdio.h> #include<math.h> #include<string.h> #define MAX 40 #define LEN 31 #define TRUE 1 #define FALSE 0 #define ERROR -1 #define TEMP -2 typedef struct { int x,y; }point; typedef struct { int rc; int x,y; }temppos; typedef struct { int min1,t,min2; }mint; int w[8][2]={0,1,1,0,0,-1,-1,0,1,1,1,-1,-1,1,-1,-1}; int m[LEN+1][LEN+1]; int xxx,yyy; int unuse[MAX]; int nowstep; int usestep[1000]; int mt; int count=0; point tele[MAX]; point tempposition; int R,T; int Initialize() { memset(m,0,(LEN+1)*(LEN+1)*sizeof(int)); memset(unuse,0,MAX*sizeof(int)); memset(usestep,0,1000*sizeof(int)); mt=0; nowstep=0; return TRUE; } int input() { int i,j; int a,b; scanf("%d %d",&R,&T); if(R==0&&T==0)return FALSE; for(i=0;i<R;i++) { scanf("%d %d",&a,&b); m[a][b]=1; } for(j=0;j<T;j++) { scanf("%d %d",&a,&b); tele[j].x=a; tele[j].y=b; unuse[j]=1; } xxx=15; yyy=15; m[xxx][yyy]=0; return TRUE; } int getlaw(int x,int y) { if(x<1||x>LEN||y<1||y>LEN)return ERROR; else return m[x][y]; } int teleempty(int i) { int sx,sy; point ss; ss=tele[i]; sx=ss.x; sy=ss.y; if(m[sx][sy]==0) return TRUE; else return FALSE; } int telestep(int i) { point s; s=tele[i]; xxx=s.x; yyy=s.y; unuse[i]=0; usestep[i]=nowstep; return TRUE; } int mindistance(int xxxx,int yyyy) { int t=100; int i,j; for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) { if(m[i][j]==1) { int u; u=abs(i-xxxx)+abs(j-yyyy); if(u<t) t=u; } } return t; } int mindis(int x,int y) {int u; u=abs(x-xxx)+abs(y-yyy); return u; } int mindis1(int x,int y,int x1,int y1) { int u; u=abs(x-x1)+abs(y-y1); return u; } int robotstep_real() { int u; int tm[LEN+1][LEN+1]; //修改为全局变量 int i,j,k,t; int ix,iy; int ii,jj; for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) tm[i][j]=m[i][j]; for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) { u=100; if(tm[i][j]==1) { for(k=0;k<8;k++)// { ii=i+w[k][0]; jj=j+w[k][1]; if(getlaw(ii,jj)==-1)continue; t=mindis(ii,jj); //计算距离 if(u>t) { u=t; ix=ii; iy=jj; } } m[i][j]=0; //? if(m[ix][iy]!=0) m[ix][iy]=2; else m[ix][iy]=1; break; } } return TRUE; } mint robotstep_simulate(int x1,int y1,int iii,int d_sign) { int u; mint mint1; int tm[LEN+1][LEN+1],tm1[LEN+1][LEN+1]; int i,j,k,t,p; int ix,iy; int ii,jj; int x2,y2; int min1=100,min2=100; for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) { tm[i][j]=m[i][j]; tm1[i][j]=m[i][j]; } tm[x1][y1]=0; if(d_sign==-3||d_sign==-4) { x2=x1+w[iii][0]; y2=w[iii][1]; tm[x2][y2]=-2; } for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) { u=100; if(tm[i][j]==1) { for(k=0;k<8;k++)// { ii=i+w[k][0]; jj=j+w[k][1]; if(getlaw(ii,jj)==-1)continue; t=mindis1(ii,jj,x1,y1); if(u>t) { u=t; ix=ii; iy=jj; } } tm1[i][j]=0; p=abs(x1-i)+abs(y1-j); if(p<min1) min1=p; if(tm1[ix][iy]!=0) tm1[ix][iy]=2; else tm1[ix][iy]=1; break; } } t=0; for(i=1;i<=LEN;i++) for(j=1;j<=LEN;j++) { if(tm1[i][j]==1) { t++; p=abs(x1-i)+abs(y1-j); if(p<min2) min2=p; } else continue; } mint1.min1=min1; mint1.min2=min2; mint1.t=t; return mint1; } int robotcount() { int t=0; int i,j; for(i=1;i<=LEN;i++) { for(j=1;j<=LEN;j++) { if(m[i][j]==1) t++; } } return t; } temppos myonestep(int i) { temppos t; int x1,y1; int x2,y2; int s,s1; x1=xxx+w[i][0]; y1=yyy+w[i][1]; s= getlaw(x1,y1); if (s<0||s==1) { t.x=0; t.y=0; t.rc=-2;//该步不合法 return t; } if (s==0) //empty { t.x=x1; t.y=y1; t.rc=-1;//进入的方格为空 return t; } x2=x1+w[i][0]; y2=y1+w[i][1]; s1=getlaw(x2,y2); if (s1<0||s1==2) { t.x=0; t.y=0; t.rc=-2;//该步不合法 return t; } if(s1==0) { t.x=x1; t.y=y1; t.rc=-3; }//下一步方格内是debris,下一个方格为空 if(s1==1) { t.x=x1; t.y=y1; t.rc=-4; }//下一步方格内是debris,下一个方格是robots return t; } int mystep_real() { int i,j,ii; mint mint1; int min_a,min_b,min1,min; int x1,y1,d_sign,ttt; temppos tp1,tp2,t; tp1.rc=100; tp1.x=0; tp1.y=0; ttt=robotcount(); min=mindistance(xxx,yyy); for(i=0;i<8;i++) { t=myonestep(i); if(t.rc==-2) continue; x1=t.x; y1=t.y; d_sign=t.rc; ii=i; mint1=robotstep_simulate(x1,y1,ii,d_sign); tp2.rc=mint1.t; min_b=mint1.min2; min1=mint1.min1; tp2.x=x1; tp2.y=y1; if(tp1.rc==tp2.rc) { if(min_b>min_a) { tp1=tp2; min_a=min_b; } } if(tp1.rc>tp2.rc) { tp1.rc=tp2.rc; tp1.x=tp2.x; tp1.y=tp2.y; min_a=min_b; } } if(tp1.rc==100) { for(j=0;j<T;j++) if(unuse[j]) { if(teleempty(j)) { telestep(j); printf("move %d:teleport to(%d,%d)\n",usestep[j],xxx,yyy); nowstep++; printf("%d %d\n",xxx,yyy); return TRUE; } else continue; } if(j==T) { nowstep++; return TEMP; } } else { if(ttt==tp1.rc&&min1>min_a) { nowstep++; printf("%d %d\n",xxx,yyy); return TRUE; } xxx=tp1.x; yyy=tp1.y; m[xxx][yyy]=0; if(d_sign==-3||d_sign==-4) { x1=xxx+w[ii][0]; y1=yyy+w[ii][1]; m[x1][y1]=2; } } nowstep++; printf("%d %d\n",xxx,yyy); return TRUE; } int debriscount() { int t=0; int i,j; for(i=1;i<=LEN;i++) { for(j=1;j<=LEN;j++) { if(m[i][j]==2)t++; } } return t; } void printlose() { int d,r; printf("Lost game after making %d moves\n",nowstep); printf("Final position(%d,%d)\n",xxx,yyy); d=debriscount(); r=robotcount(); printf("Number of robots remaining: %d\n",r); printf("Number of cells with debris: %d\n",d); } void printwin() { int p; printf("Win game after making %d moves\n",nowstep); printf("Final position(%d,%d)\n",xxx,yyy); p=debriscount(); printf("Number of cells with debris: %d\n",p); } int main() { int t; while(Initialize()&&input()) { count++; printf("Case %d:\n",count); while(robotcount()) { t=mystep_real(); if(t==-2) { while(robotcount()!=0&&m[xxx][yyy]!=0) { robotstep_real(); } if(robotcount()==0&&m[xxx][yyy]!=0) { printwin(); break; } else { printlose(); break; } } if(t==1) { robotstep_real(); if(robotcount()==0) { printwin(); break; } if(m[xxx][yyy]!=0) { printlose(); break; } } } } return TRUE; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator