Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
adc2018-system
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
adc2018
adc2018-system
Compare Revisions
master...avoid_recrusion
Source
avoid_recrusion
Select Git revision
...
Target
master
Select Git revision
Compare
Commits (2)
not done
· 56a93b0f
royus
authored
Aug 27, 2018
56a93b0f
not done
· c6c67e0a
royus
authored
Aug 27, 2018
c6c67e0a
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
162 additions
and
134 deletions
+162
-134
solver.c
source/solver.c
+162
-134
No files found.
source/solver.c
View file @
c6c67e0a
/* solver.c */
/* Last Change: 2018/08/2
6 (Sun) 23:31:34
. */
/* Last Change: 2018/08/2
7 (Mon) 23:42:09
. */
#define MAX_ATTEMPS 10000
0
#define MAX_ATTEMPS 10000
#include<stdio.h>
#include<stdlib.h>
...
...
@@ -16,6 +16,11 @@ 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
...
...
@@ -73,129 +78,143 @@ 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
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
;
if
(
avail
[
nowz
][
nowy
][
nowx
]
!=
1
&&
flag
==
0
)
return
0
;
int
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
<
width
-
1
&&
avail
[
nowz
][
nowy
][
nowx
+
1
]
>=
2
)
isbranch
++
;
if
(
nowy
<
height
-
1
&&
avail
[
nowz
][
nowy
+
1
][
nowx
]
>=
2
)
isbranch
++
;
if
(
nowz
<
depth
-
1
&&
avail
[
nowz
+
1
][
nowy
][
nowx
]
>=
2
)
isbranch
++
;
if
(
isbranch
>
0
){
avail
[
nowz
][
nowy
][
nowx
]
=
1
;
return
0
;
}
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
++
;
}
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
<
goalx
){
searchorder
[
src
]
=
3
;
src
++
;
}
else
searchorder
[
2
+
src
]
=
3
;
if
(
nowy
<
goaly
){
searchorder
[
src
]
=
4
;
src
++
;
}
else
searchorder
[
1
+
src
]
=
4
;
if
(
nowz
<
goalz
){
searchorder
[
src
]
=
5
;
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
);
t
=
searchorder
[
i
];
searchorder
[
i
]
=
searchorder
[
j
];
searchorder
[
j
]
=
t
;
}
for
(
i
=
src
;
i
<
6
;
i
++
){
j
=
src
+
rand
()
%
(
6
-
src
);
t
=
searchorder
[
i
];
searchorder
[
i
]
=
searchorder
[
j
];
searchorder
[
j
]
=
t
;
}
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 ",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
;
/* 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
;
}
if
(
avail
[
nowz
][
nowy
][
nowx
]
==
2
)
avail
[
nowz
][
nowy
][
nowx
]
=
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
<
width
-
1
&&
avail
[
nowz
][
nowy
][
nowx
+
1
]
>=
2
)
isbranch
++
;
if
(
nowy
<
height
-
1
&&
avail
[
nowz
][
nowy
+
1
][
nowx
]
>=
2
)
isbranch
++
;
if
(
nowz
<
depth
-
1
&&
avail
[
nowz
+
1
][
nowy
][
nowx
]
>=
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;j<index;j++) */
/* printf("%d ",history[j]); */
/* printf("\n"); */
/* return 0;//goback */
continue
;
}
}
if
(
avail
[
nowz
][
nowy
][
nowx
]
==
1
)
avail
[
nowz
][
nowy
][
nowx
]
=
2
;
if
(
nowx
>
goalx
){
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
<
goalx
){
searchorder
[
src
]
=
3
;
src
++
;
}
else
searchorder
[
2
+
src
]
=
3
;
if
(
nowy
<
goaly
){
searchorder
[
src
]
=
4
;
src
++
;
}
else
searchorder
[
1
+
src
]
=
4
;
if
(
nowz
<
goalz
){
searchorder
[
src
]
=
5
;
src
++
;
}
else
searchorder
[
src
]
=
5
;
for
(
i
=
0
;
i
<
src
;
i
++
){
j
=
rand
()
%
(
src
);
t
=
searchorder
[
i
];
searchorder
[
i
]
=
searchorder
[
j
];
searchorder
[
j
]
=
t
;
}
for
(
i
=
src
;
i
<
6
;
i
++
){
j
=
src
+
rand
()
%
(
6
-
src
);
t
=
searchorder
[
i
];
searchorder
[
i
]
=
searchorder
[
j
];
searchorder
[
j
]
=
t
;
}
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]]); */
history
[
index
]
=
searchorder
[
i
];
/* printf("%d %d %d +%d %d\n",nowx,nowy,nowz,searchorder[i],index+1); */
/* for(j=0;j<index;j++) */
/* printf("%d ",history[j]); */
/* printf("\n"); */
index
++
;
}
}
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
){
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<index;j++) */
/* printf("%d ",history[j]); */
/* printf("\n"); */
/* printf("%d %d %d\n",dx[history[index]],dy[history[index]],dz[history[index]]); */
continue
;
}
}
}
return
0
;
}
...
...
@@ -228,7 +247,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
(
start
x
,
starty
,
startz
,
1
);
return
available
(
start
z
,
starty
,
startx
);
}
void
connect
(
int
line
){
//connect line
...
...
@@ -256,9 +275,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
=
rand
line
()
;
linea
=
rand
()
%
lines
+
1
;
if
(
connected
[
linea
])
continue
;
if
(
connectable
(
linea
,
0
)){
//if linea canbe conneced without deleting lines
...
...
@@ -267,19 +286,28 @@ int solve(void){ //return 1 when solved, 0 when cannot
connected
[
0
]
++
;
if
(
connected
[
0
]
==
lines
)
return
1
;
}
else
{
for
(
j
=
0
;
j
<
MAX_ATTEMPS
;
j
++
){
//try to find a line to delete
lineb
=
randline
();
if
(
connectable
(
linea
,
lineb
)){
delete
(
lineb
);
}
else
{
int
j
,
t
;
for
(
i
=
0
;
i
<
lines
;
i
++
){
lineb
[
i
]
=
i
+
1
;
}
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
;
}
}
}
}
...
...