Commit 56a93b0f authored by royus's avatar royus

not done

parent 88f47819
/* solver.c */ /* solver.c */
/* Last Change: 2018/08/26 (Sun) 23:31:34. */ /* Last Change: 2018/08/27 (Mon) 23:01:56. */
#define MAX_ATTEMPS 100000 #define MAX_ATTEMPS 10000
#include<stdio.h> #include<stdio.h>
#include<stdlib.h> #include<stdlib.h>
...@@ -16,6 +16,9 @@ int connected[8*72*72/2+1]={}; //connected[0]=(number of connected lines) ...@@ -16,6 +16,9 @@ int connected[8*72*72/2+1]={}; //connected[0]=(number of connected lines)
int depth,height,width; int depth,height,width;
int goalx,goaly,goalz; int goalx,goaly,goalz;
int lines; 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};
//z,y,x //z,y,x
void read(void){ //read problem void read(void){ //read problem
...@@ -73,28 +76,25 @@ void read(void){ //read problem ...@@ -73,28 +76,25 @@ void read(void){ //read problem
return; return;
} }
int randline(void){ //return random line number int available(int z,int y, int x){ //return 1 when (nowx,nowy,nowz) can be connected to goal
return rand()%lines+1; int i,src=0,contd,isbranch;
} int history[8*72*72],index=0;
int nowx=x,nowy=y,nowz=z;
void shuffle(int array[],int start,int end){ int searchorder[6]; //x+-,y+-,z+-
int i,j,t; /* for(i=0;i<6;i++) */
if(end==start) /* printf("%d %d %d\n",dx[i],dy[i],dz[i]); */
return; while(1){
for(i=start;i<end;i++){ /* printf("%d %d %d\n",nowx,nowy,nowz); */
j=start+rand()%(end-start); if(nowx<0||nowy<0||nowz<0||nowx>=width||nowy>=height||nowz>=depth){
t=array[i]; printf("error %d %d %d\n",nowx,nowy,nowz);
array[i]=array[j]; /* printf("%d, %d\n",index,history[index]); */
array[j]=t; /* printf("%d %d %d\n",dx[history[index]],dy[history[index]],dz[history[index]]); */
return 0;
} }
} if(avail[nowz][nowy][nowx]==-1){ //goal
int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,nowz) can be connected to goal
if(avail[nowz][nowy][nowx]==-1) //goal
return 1; return 1;
if(avail[nowz][nowy][nowx]!=1&&flag==0) }
return 0; isbranch=-1;
int isbranch=-1;
if(nowx>0&&avail[nowz][nowy][nowx-1]>=2) if(nowx>0&&avail[nowz][nowy][nowx-1]>=2)
isbranch++; isbranch++;
if(nowy>0&&avail[nowz][nowy-1][nowx]>=2) if(nowy>0&&avail[nowz][nowy-1][nowx]>=2)
...@@ -107,16 +107,23 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy, ...@@ -107,16 +107,23 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,
isbranch++; isbranch++;
if(nowz<depth-1&&avail[nowz+1][nowy][nowx]>=2) if(nowz<depth-1&&avail[nowz+1][nowy][nowx]>=2)
isbranch++; isbranch++;
if(isbranch>0){ 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){
/* printf("something wrong\n"); */
}else{
nowx-=dx[history[index]];
nowy-=dy[history[index]];
nowz-=dz[history[index]];
avail[nowz][nowy][nowx]=1; avail[nowz][nowy][nowx]=1;
return 0; printf("%d %d %d -\n",nowx,nowy,nowz);
/* return 0;//goback */
continue;
}
} }
if(avail[nowz][nowy][nowx]==1) if(avail[nowz][nowy][nowx]==1)
avail[nowz][nowy][nowx]=2; avail[nowz][nowy][nowx]=2;
int i,src=0;
int searchorder[6]; //x+-,y+-,z+-
/* for(i=0;i<6;i++) */
/* searchorder[i]=-1; */
if(nowx>goalx){ if(nowx>goalx){
searchorder[src]=0; searchorder[src]=0;
src++; src++;
...@@ -147,8 +154,6 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy, ...@@ -147,8 +154,6 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,
src++; src++;
}else }else
searchorder[src]=5; searchorder[src]=5;
/* shuffle(searchorder,0,src); */
/* shuffle(searchorder,src,6); */
int j,t; int j,t;
for(i=0;i<src;i++){ for(i=0;i<src;i++){
j=rand()%(src); j=rand()%(src);
...@@ -162,40 +167,41 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy, ...@@ -162,40 +167,41 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,
searchorder[i]=searchorder[j]; searchorder[i]=searchorder[j];
searchorder[j]=t; searchorder[j]=t;
} }
/* for(i=0;i<6;i++) */ contd=1;
/* printf("%d ",searchorder[i]); */ for(i=0;i<6&&contd;i++){
/* printf("\n"); */ /* printf("%d\n",searchorder[i]); */
for(i=0;i<6;i++) if(nowx+dx[searchorder[i]]<0||nowy+dy[searchorder[i]]<0||nowz+dx[searchorder[i]]<0||nowx+dz[searchorder[i]]>=width||nowy+dy[searchorder[i]]>=height||nowz+dz[searchorder[i]]>=depth)
switch(searchorder[i]){ continue;
case 0: if(avail[nowz+dz[searchorder[i]]][nowy+dy[searchorder[i]]][nowx+dx[searchorder[i]]]==1){
if(nowx>0&&available(nowx-1,nowy,nowz,0)) contd=0;
return 1; nowx+=dx[searchorder[i]];
break; nowy+=dy[searchorder[i]];
case 1: nowz+=dz[searchorder[i]];
if(nowy>0&&available(nowx,nowy-1,nowz,0)) /* printf("%d %d %d\n",dx[searchorder[i]],dy[searchorder[i]],dz[searchorder[i]]); */
return 1; printf("%d %d %d +\n",nowx,nowy,nowz);
break; history[index]=searchorder[i];
case 2: /* printf("%d\n",index); */
if(nowz>0&&available(nowx,nowy,nowz-1,0)) index++;
return 1; }
break;
case 3:
if(nowx<width-1&&available(nowx+1,nowy,nowz,0))
return 1;
break;
case 4:
if(nowy<height-1&&available(nowx,nowy+1,nowz,0))
return 1;
break;
case 5:
if(nowz<depth-1&&available(nowx,nowy,nowz+1,0))
return 1;
break;
default:
break;
} }
if(avail[nowz][nowy][nowx]==2) if(avail[nowz][nowy][nowx]==3||index==0)
return 0;
if(contd&&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){
/* printf("something wrong\n"); */
}else{
nowx-=dx[history[index]];
nowy-=dy[history[index]];
nowz-=dz[history[index]];
avail[nowz][nowy][nowx]=1; avail[nowz][nowy][nowx]=1;
printf("%d %d %d -\n",nowx,nowy,nowz);
/* printf("%d %d %d\n",dx[history[index]],dy[history[index]],dz[history[index]]); */
continue;
}
}
}
return 0; return 0;
} }
...@@ -228,7 +234,7 @@ int connectable(int linea,int lineb){ //return 1 if linea can be connected when ...@@ -228,7 +234,7 @@ int connectable(int linea,int lineb){ //return 1 if linea can be connected when
printf("Error: Line %d\n",linea); printf("Error: Line %d\n",linea);
return 0; return 0;
} }
return available(startx,starty,startz,1); return available(startz,starty,startx);
} }
void connect(int line){ //connect line void connect(int line){ //connect line
...@@ -256,9 +262,9 @@ void delete(int line){ //delete line ...@@ -256,9 +262,9 @@ void delete(int line){ //delete line
} }
int solve(void){ //return 1 when solved, 0 when cannot int solve(void){ //return 1 when solved, 0 when cannot
int i,j,linea,lineb; int i,linea,lineb[10000];
for(i=0;i<MAX_ATTEMPS;i++){ //try to connect MAX_ATTEMPS times for(i=0;i<MAX_ATTEMPS;i++){ //try to connect MAX_ATTEMPS times
linea=randline(); linea=rand()%lines+1;
if(connected[linea]) if(connected[linea])
continue; continue;
if(connectable(linea,0)){ //if linea canbe conneced without deleting lines if(connectable(linea,0)){ //if linea canbe conneced without deleting lines
...@@ -267,22 +273,31 @@ int solve(void){ //return 1 when solved, 0 when cannot ...@@ -267,22 +273,31 @@ int solve(void){ //return 1 when solved, 0 when cannot
connected[0]++; connected[0]++;
if(connected[0]==lines) if(connected[0]==lines)
return 1; return 1;
}else{
int j,t;
for(i=0;i<lines;i++){
lineb[i]=i+1;
} }
else{ for(i=0;i<lines;i++){ //shuffle
for(j=0;j<MAX_ATTEMPS;j++){ //try to find a line to delete j=rand()%(lines);
lineb=randline(); t=lineb[i];
if(connectable(linea,lineb)){ lineb[i]=lineb[j];
delete(lineb); lineb[j]=t;
}
for(i=0;i<lines;i++){
if(connectable(linea,lineb[i])){
delete(lineb[i]);
connect(linea); connect(linea);
connected[linea]=1; connected[linea]=1;
connected[lineb]=0; connected[lineb[i]]=0;
break; break;
} }
if(j==MAX_ATTEMPS-1) if(j==MAX_ATTEMPS-1){
return 0; return 0;
} }
} }
} }
}
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment