/* solver.c */ /* Last Change: 2018/08/27 (Mon) 23:42:09. */ #define MAX_ATTEMPS 10000 #include #include #include /* #include */ /* #include */ #include int board[8][72][72]={}; int avail[8][72][72]={}; //start=3,path=2,avail=1,nonavail=0,goal=-1 int connected[8*72*72/2+1]={}; //connected[0]=(number of connected lines) int depth,height,width; int goalx,goaly,goalz; int lines; int dx[6]={-1,0,0,1,0,0}; int dy[6]={0,-1,0,0,1,0}; int dz[6]={0,0,-1,0,0,1}; int history[8*72*72]; int searchorder[6]; //x+-,y+-,z+- //z,y,x void read(void){ //read problem int x,y,z,i; char c,str[8]; scanf(" %s",str); //SIZE scanf(" %d",&width); if(width>72||width<=0) printf("Error: width\n"); scanf(" %c",&c); //X scanf(" %d",&height); if(height>72||height<=0) printf("Error: height\n"); scanf(" %c",&c); //X scanf(" %d",&depth); if(depth>8||depth<=0) printf("Error: depth\n"); scanf(" %s",str); //LINE_NUM scanf(" %d",&lines); if(lines<=0) printf("Error: lines\n"); for(i=1;i<=lines;i++){ scanf(" %s",str); //LINE#X scanf(" %c",&c); //( scanf(" %d",&x); if(x>=72||x<0) printf("Error: x\n"); scanf(" %c",&c); //, scanf(" %d",&y); if(y>=72||y<0) printf("Error: y\n"); scanf(" %c",&c); //, scanf(" %d",&z); if(z>=72||z<0) printf("Error: z\n"); scanf(" %c",&c); //) board[z-1][y][x]=-i; scanf("%c",&c); //) /* scanf(" %[ -]",&c); //space or - */ scanf(" %c",&c); //( scanf(" %d",&x); if(x>=72||x<0) printf("Error: x\n"); scanf(" %c",&c); //, scanf(" %d",&y); if(y>=72||y<0) printf("Error: y\n"); scanf(" %c",&c); //, scanf(" %d",&z); if(z>=72||z<0) printf("Error: z\n"); scanf(" %c",&c); //) board[z-1][y][x]=-i; } return; } int available(int z,int y, int x){ //return 1 when (nowx,nowy,nowz) can be connected to goal int i,j,t,src=0,contd,isbranch; int index=0; int nowx=x,nowy=y,nowz=z; /* for(i=0;i<6;i++) */ /* printf("%d %d %d\n",dx[i],dy[i],dz[i]); */ for(i=0;i<8*72*72;i++) history[i]=-1; index=0; while(1){ /* printf("%d %d %d\n",nowx,nowy,nowz); */ if(nowx<0||nowy<0||nowz<0||nowx>=width||nowy>=height||nowz>=depth){ printf("error %d %d %d\n",nowx,nowy,nowz); /* printf("%d, %d\n",index,history[index]); */ /* printf("%d %d %d\n",dx[history[index]],dy[history[index]],dz[history[index]]); */ return 0; } if(avail[nowz][nowy][nowx]==-1){ //goal return 1; } isbranch=-1; if(nowx>0&&avail[nowz][nowy][nowx-1]>=2) isbranch++; if(nowy>0&&avail[nowz][nowy-1][nowx]>=2) isbranch++; if(nowz>0&&avail[nowz-1][nowy][nowx]>=2) isbranch++; if(nowx=2) isbranch++; if(nowy=2) isbranch++; if(nowz=2) isbranch++; if(isbranch>0&&index){ avail[nowz][nowy][nowx]=0; index--; if(nowx-dx[history[index]]<0||nowy-dy[history[index]]<0||nowz-dx[history[index]]<0||nowx-dz[history[index]]>=width||nowy-dy[history[index]]>=height||nowz-dz[history[index]]>=depth){ index++; /* printf("something wrong\n"); */ }else{ nowx-=dx[history[index]]; nowy-=dy[history[index]]; nowz-=dz[history[index]]; avail[nowz][nowy][nowx]=1; /* printf("%d %d %d -%d %d\n",nowx,nowy,nowz,history[index],index); */ /* for(j=0;jgoalx){ searchorder[src]=0; src++; }else searchorder[5+src]=0; if(nowy>goaly){ searchorder[src]=1; src++; }else searchorder[4+src]=1; if(nowz>goalz){ searchorder[src]=2; src++; }else searchorder[3+src]=2; if(nowx=width||nowy+dy[searchorder[i]]>=height||nowz+dz[searchorder[i]]>=depth) continue; if(avail[nowz+dz[searchorder[i]]][nowy+dy[searchorder[i]]][nowx+dx[searchorder[i]]]==1){ contd=0; nowx+=dx[searchorder[i]]; nowy+=dy[searchorder[i]]; nowz+=dz[searchorder[i]]; /* printf("%d %d %d\n",dx[searchorder[i]],dy[searchorder[i]],dz[searchorder[i]]); */ history[index]=searchorder[i]; /* printf("%d %d %d +%d %d\n",nowx,nowy,nowz,searchorder[i],index+1); */ /* for(j=0;j=width||nowy-dy[history[index]]>=height||nowz-dz[history[index]]>=depth){ index++; /* printf("something wrong\n"); */ }else{ nowx-=dx[history[index]]; nowy-=dy[history[index]]; nowz-=dz[history[index]]; avail[nowz][nowy][nowx]=1; /* printf("%d %d %d -%d %d\n",nowx,nowy,nowz,history[index],index); */ /* for(j=0;j