Commit 56a93b0f authored by royus's avatar royus
Browse files

not done

parent 88f47819
Loading
Loading
Loading
Loading
+87 −72
Changes for source/solver.c: 87 added lines, 72 removed lines.
Original line number Diff line number Diff line
/* 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<stdlib.h>
@@ -16,6 +16,9 @@ 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};
//z,y,x

void read(void){ //read problem
@@ -73,28 +76,25 @@ void read(void){ //read problem
	return;
}

int randline(void){ //return random line number
	return rand()%lines+1;
}

void shuffle(int array[],int start,int end){
	int i,j,t;
	if(end==start)
		return;
	for(i=start;i<end;i++){
		j=start+rand()%(end-start);
		t=array[i];
		array[i]=array[j];
		array[j]=t;
	}
int available(int z,int y, int x){ //return 1 when (nowx,nowy,nowz) can be connected to goal
	int i,src=0,contd,isbranch;
	int history[8*72*72],index=0;
	int nowx=x,nowy=y,nowz=z;
	int searchorder[6]; //x+-,y+-,z+-
	/* for(i=0;i<6;i++) */
	/* printf("%d %d %d\n",dx[i],dy[i],dz[i]); */
	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;
		}

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
		if(avail[nowz][nowy][nowx]==-1){ //goal
			return 1;
	if(avail[nowz][nowy][nowx]!=1&&flag==0)
		return 0;
	int isbranch=-1;
		}
		isbranch=-1;
		if(nowx>0&&avail[nowz][nowy][nowx-1]>=2)
			isbranch++;
		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,
			isbranch++;
		if(nowz<depth-1&&avail[nowz+1][nowy][nowx]>=2)
			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;
		return 0;
				printf("%d %d %d -\n",nowx,nowy,nowz);
				/* return 0;//goback */
				continue;
			}
		}
		if(avail[nowz][nowy][nowx]==1)
			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){
			searchorder[src]=0;
			src++;
@@ -147,8 +154,6 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,
			src++;
		}else
			searchorder[src]=5;
	/* shuffle(searchorder,0,src); */
	/* shuffle(searchorder,src,6); */
		int j,t;
		for(i=0;i<src;i++){
			j=rand()%(src);
@@ -162,40 +167,41 @@ int available(int nowx,int nowy, int nowz,int flag){ //return 1 when (nowx,nowy,
			searchorder[i]=searchorder[j];
			searchorder[j]=t;
		}
	/* for(i=0;i<6;i++) */
	/* 	printf("%d ",searchorder[i]); */
	/* printf("\n"); */
	for(i=0;i<6;i++)
		switch(searchorder[i]){
			case 0:
				if(nowx>0&&available(nowx-1,nowy,nowz,0))
					return 1;
				break;
			case 1:
				if(nowy>0&&available(nowx,nowy-1,nowz,0))
					return 1;
				break;
			case 2:
				if(nowz>0&&available(nowx,nowy,nowz-1,0))
					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;
		contd=1;
		for(i=0;i<6&&contd;i++){
			/* printf("%d\n",searchorder[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)
				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]]); */
				printf("%d %d %d +\n",nowx,nowy,nowz);
				history[index]=searchorder[i];
				/* printf("%d\n",index); */
				index++;
			}
		}
	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;
				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;
}

@@ -228,7 +234,7 @@ int connectable(int linea,int lineb){ //return 1 if linea can be connected when
		printf("Error: Line %d\n",linea);
		return 0;
	}
	return available(startx,starty,startz,1);
	return available(startz,starty,startx);
}

void connect(int line){ //connect line
@@ -256,9 +262,9 @@ void delete(int line){ //delete line
}

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
		linea=randline();
		linea=rand()%lines+1;
		if(connected[linea])
			continue;
		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
			connected[0]++;
			if(connected[0]==lines)
				return 1;
		}else{
			int j,t;
			for(i=0;i<lines;i++){
				lineb[i]=i+1;
			}
		else{
			for(j=0;j<MAX_ATTEMPS;j++){ //try to find a line to delete
				lineb=randline();
				if(connectable(linea,lineb)){
					delete(lineb);
			for(i=0;i<lines;i++){ //shuffle
				j=rand()%(lines);
				t=lineb[i];
				lineb[i]=lineb[j];
				lineb[j]=t;
			}
			for(i=0;i<lines;i++){
				if(connectable(linea,lineb[i])){
					delete(lineb[i]);
					connect(linea);
					connected[linea]=1;
					connected[lineb]=0;
					connected[lineb[i]]=0;
					break;
				}
				if(j==MAX_ATTEMPS-1)
				if(j==MAX_ATTEMPS-1){
					return 0;
				}
			}
		}
	}
	return 0;
}