Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
twd-solver
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
adc2019
twd-solver
Commits
a1f4bafd
Commit
a1f4bafd
authored
Aug 27, 2019
by
tawada
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimize blank block
parent
6773ea6a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
213 additions
and
43 deletions
+213
-43
solver.py
solver.py
+213
-43
No files found.
solver.py
View file @
a1f4bafd
...
@@ -206,15 +206,222 @@ def print_cnf(nodes, cnfs, filename='p.txt'):
...
@@ -206,15 +206,222 @@ def print_cnf(nodes, cnfs, filename='p.txt'):
#print(''.join(s))
#print(''.join(s))
def
print_cnf2
(
nodes
,
cnfs
,
filename
=
'p.txt'
):
def
print_cnf2
(
nodes
,
cnfs
,
filename
=
'p.txt'
):
num
=
len
(
nodes
)
s
=
[]
s
=
[]
s
.
append
(
f
'p cnf {
num
} {len(cnfs)}
\n
'
)
s
.
append
(
f
'p cnf {
len(nodes)
} {len(cnfs)}
\n
'
)
for
cnf
in
cnfs
:
for
cnf
in
cnfs
:
cnf
=
[
str
(
i
)
for
i
in
cnf
]
cnf
=
[
str
(
i
)
for
i
in
cnf
]
s
.
append
((
' '
.
join
(
cnf
))
+
' 0
\n
'
)
s
.
append
((
' '
.
join
(
cnf
))
+
' 0
\n
'
)
with
open
(
filename
,
'w'
)
as
file
:
with
open
(
filename
,
'w'
)
as
file
:
file
.
write
(
''
.
join
(
s
))
file
.
write
(
''
.
join
(
s
))
def
_get_map_b
(
Q
,
A
):
map_b
=
np
.
zeros
((
A
[
'h'
],
A
[
'w'
]),
dtype
=
int
)
for
b
in
range
(
1
,
Q
[
'num_b'
]
+
1
):
x
=
A
[
'BLOCK'
][
b
][
'x'
]
y
=
A
[
'BLOCK'
][
b
][
'y'
]
for
dx
,
dy
in
Q
[
'BLOCK'
][
b
][
'cells'
]
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
b
return
map_b
def
optimize
(
Q
,
A
):
Q
,
A
=
del_blank
(
Q
,
A
)
num_b
=
Q
[
'num_b'
]
num_l
=
Q
[
'num_l'
]
map_l
=
A
[
'map'
]
h
,
w
=
map_l
.
shape
map_b
=
_get_map_b
(
Q
,
A
)
# 左に動かす
while
True
:
updated
=
False
for
b
in
range
(
1
,
num_b
+
1
):
cells
=
Q
[
'BLOCK'
][
b
][
'cells'
]
x
,
y
=
A
[
'BLOCK'
][
b
][
'x'
],
A
[
'BLOCK'
][
b
][
'y'
]
canMove
=
True
for
dx
,
dy
in
cells
.
keys
():
if
cells
[(
dx
,
dy
)]
!=
'+'
:
# とりあえず配線のあるブロックは動かさない
canMove
=
False
break
if
x
+
dx
-
1
<
0
:
canMove
=
False
break
if
map_b
[
y
+
dy
,
x
+
dx
-
1
]
!=
0
and
map_b
[
y
+
dy
,
x
+
dx
-
1
]
!=
b
:
canMove
=
False
break
if
map_l
[
y
+
dy
,
x
+
dx
-
1
]
!=
0
:
canMove
=
False
break
if
not
canMove
:
continue
# 左に動かせる!
A
[
'BLOCK'
][
b
][
'x'
]
=
x
-
1
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
0
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
-
1
]
=
b
updated
=
True
if
not
updated
:
break
else
:
Q
,
A
=
del_blank
(
Q
,
A
)
map_b
=
_get_map_b
(
Q
,
A
)
h
,
w
=
map_b
.
shape
# 上に動かす
while
True
:
updated
=
False
for
b
in
range
(
1
,
num_b
+
1
):
cells
=
Q
[
'BLOCK'
][
b
][
'cells'
]
x
,
y
=
A
[
'BLOCK'
][
b
][
'x'
],
A
[
'BLOCK'
][
b
][
'y'
]
canMove
=
True
for
dx
,
dy
in
cells
.
keys
():
if
cells
[(
dx
,
dy
)]
!=
'+'
:
# とりあえず配線のあるブロックは動かさない
canMove
=
False
break
if
y
+
dy
-
1
<
0
:
canMove
=
False
break
if
map_b
[
y
+
dy
-
1
,
x
+
dx
]
!=
0
and
map_b
[
y
+
dy
-
1
,
x
+
dx
]
!=
b
:
canMove
=
False
break
if
map_l
[
y
+
dy
-
1
,
x
+
dx
]
!=
0
:
canMove
=
False
break
if
not
canMove
:
continue
# 上に動かせる!
A
[
'BLOCK'
][
b
][
'y'
]
=
y
-
1
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
0
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
-
1
,
x
+
dx
]
=
b
updated
=
True
if
not
updated
:
break
else
:
Q
,
A
=
del_blank
(
Q
,
A
)
map_b
=
_get_map_b
(
Q
,
A
)
h
,
w
=
map_b
.
shape
# 右に動かす
while
True
:
updated
=
False
for
b
in
range
(
1
,
num_b
+
1
):
cells
=
Q
[
'BLOCK'
][
b
][
'cells'
]
x
,
y
=
A
[
'BLOCK'
][
b
][
'x'
],
A
[
'BLOCK'
][
b
][
'y'
]
canMove
=
True
for
dx
,
dy
in
cells
.
keys
():
if
cells
[(
dx
,
dy
)]
!=
'+'
:
# とりあえず配線のあるブロックは動かさない
canMove
=
False
break
if
x
+
dx
+
1
>=
w
:
canMove
=
False
break
if
map_b
[
y
+
dy
,
x
+
dx
+
1
]
!=
0
and
map_b
[
y
+
dy
,
x
+
dx
+
1
]
!=
b
:
canMove
=
False
break
if
map_l
[
y
+
dy
,
x
+
dx
+
1
]
!=
0
:
canMove
=
False
break
if
not
canMove
:
continue
# 右に動かせる!
A
[
'BLOCK'
][
b
][
'x'
]
=
x
+
1
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
0
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
+
1
]
=
b
updated
=
True
if
not
updated
:
break
else
:
Q
,
A
=
del_blank
(
Q
,
A
)
map_b
=
_get_map_b
(
Q
,
A
)
h
,
w
=
map_b
.
shape
# 下に動かす
while
True
:
updated
=
False
for
b
in
range
(
1
,
num_b
+
1
):
cells
=
Q
[
'BLOCK'
][
b
][
'cells'
]
x
,
y
=
A
[
'BLOCK'
][
b
][
'x'
],
A
[
'BLOCK'
][
b
][
'y'
]
canMove
=
True
for
dx
,
dy
in
cells
.
keys
():
if
cells
[(
dx
,
dy
)]
!=
'+'
:
# とりあえず配線のあるブロックは動かさない
canMove
=
False
break
if
y
+
dy
+
1
>=
h
:
canMove
=
False
break
if
map_b
[
y
+
dy
+
1
,
x
+
dx
]
!=
0
and
map_b
[
y
+
dy
+
1
,
x
+
dx
]
!=
b
:
canMove
=
False
break
if
map_l
[
y
+
dy
+
1
,
x
+
dx
]
!=
0
:
canMove
=
False
break
if
not
canMove
:
continue
# 上に動かせる!
A
[
'BLOCK'
][
b
][
'y'
]
=
y
+
1
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
0
for
dx
,
dy
in
cells
.
keys
():
map_b
[
y
+
dy
+
1
,
x
+
dx
]
=
b
updated
=
True
if
not
updated
:
break
else
:
Q
,
A
=
del_blank
(
Q
,
A
)
map_b
=
_get_map_b
(
Q
,
A
)
h
,
w
=
map_b
.
shape
return
A
def
del_blank
(
Q
,
A
):
num_b
=
Q
[
'num_b'
]
num_l
=
Q
[
'num_l'
]
## 最小矩形に更新
map_l
=
A
[
'map'
]
h
,
w
=
map_l
.
shape
map_b
=
np
.
zeros
((
h
,
w
),
dtype
=
int
)
for
b
in
range
(
1
,
num_b
+
1
):
x
=
A
[
'BLOCK'
][
b
][
'x'
]
y
=
A
[
'BLOCK'
][
b
][
'y'
]
for
dx
,
dy
in
Q
[
'BLOCK'
][
b
][
'cells'
]
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
b
i
=
0
while
True
:
if
i
>=
h
:
break
if
(
map_l
[
i
,:]
==
0
)
.
all
()
and
(
map_b
[
i
,:]
==
0
)
.
all
():
map_l
=
np
.
delete
(
map_l
,
i
,
axis
=
0
)
map_b
=
np
.
delete
(
map_b
,
i
,
axis
=
0
)
for
b
in
range
(
1
,
num_b
+
1
):
if
A
[
'BLOCK'
][
b
][
'y'
]
>
i
:
A
[
'BLOCK'
][
b
][
'y'
]
-=
1
h
-=
1
i
-=
1
i
+=
1
j
=
0
while
True
:
if
j
>=
w
:
break
if
(
map_l
[:,
j
]
==
0
)
.
all
()
and
(
map_b
[:,
j
]
==
0
)
.
all
():
map_l
=
np
.
delete
(
map_l
,
j
,
axis
=
1
)
map_b
=
np
.
delete
(
map_b
,
j
,
axis
=
1
)
for
b
in
range
(
1
,
num_b
+
1
):
if
A
[
'BLOCK'
][
b
][
'x'
]
>
j
:
A
[
'BLOCK'
][
b
][
'x'
]
-=
1
w
-=
1
j
-=
1
j
+=
1
A
[
'map'
]
=
map_l
A
[
'h'
]
=
h
A
[
'w'
]
=
w
return
Q
,
A
def
read_satA
(
filename
,
Q
,
nodes
,
TF
=
None
):
def
read_satA
(
filename
,
Q
,
nodes
,
TF
=
None
):
with
open
(
filename
,
'r'
)
as
file
:
with
open
(
filename
,
'r'
)
as
file
:
str_line
=
file
.
readline
()
str_line
=
file
.
readline
()
...
@@ -302,45 +509,7 @@ def read_satA(filename, Q, nodes, TF=None):
...
@@ -302,45 +509,7 @@ def read_satA(filename, Q, nodes, TF=None):
check_flag
[
c
]
=
1
check_flag
[
c
]
=
1
# validation_mapが0 => 端点と接続していない => 閉路
# validation_mapが0 => 端点と接続していない => 閉路
A
[
'map'
]
=
A
[
'map'
]
*
validation_map
A
[
'map'
]
=
A
[
'map'
]
*
validation_map
A
=
optimize
(
Q
,
A
)
## 最小矩形に更新
map_l
=
A
[
'map'
]
h
,
w
=
map_l
.
shape
map_b
=
np
.
zeros
((
h
,
w
),
dtype
=
int
)
for
b
in
range
(
1
,
num_b
+
1
):
x
=
A
[
'BLOCK'
][
b
][
'x'
]
y
=
A
[
'BLOCK'
][
b
][
'y'
]
for
dx
,
dy
in
Q
[
'BLOCK'
][
b
][
'cells'
]
.
keys
():
map_b
[
y
+
dy
,
x
+
dx
]
=
b
i
=
0
while
True
:
if
i
>=
h
:
break
if
(
map_l
[
i
,:]
==
0
)
.
all
()
and
(
map_b
[
i
,:]
==
0
)
.
all
():
map_l
=
np
.
delete
(
map_l
,
i
,
axis
=
0
)
map_b
=
np
.
delete
(
map_b
,
i
,
axis
=
0
)
for
b
in
range
(
1
,
num_b
+
1
):
if
A
[
'BLOCK'
][
b
][
'y'
]
>
i
:
A
[
'BLOCK'
][
b
][
'y'
]
-=
1
h
-=
1
i
-=
1
i
+=
1
j
=
0
while
True
:
if
j
>=
w
:
break
if
(
map_l
[:,
j
]
==
0
)
.
all
()
and
(
map_b
[:,
j
]
==
0
)
.
all
():
map_l
=
np
.
delete
(
map_l
,
j
,
axis
=
1
)
map_b
=
np
.
delete
(
map_b
,
j
,
axis
=
1
)
for
b
in
range
(
1
,
num_b
+
1
):
if
A
[
'BLOCK'
][
b
][
'x'
]
>
j
:
A
[
'BLOCK'
][
b
][
'x'
]
-=
1
w
-=
1
j
-=
1
j
+=
1
A
[
'map'
]
=
map_l
A
[
'h'
]
=
h
A
[
'w'
]
=
w
return
A
return
A
def
read_satA2
(
filename
,
Q
,
nodes
,
TF
=
None
):
def
read_satA2
(
filename
,
Q
,
nodes
,
TF
=
None
):
...
@@ -1390,15 +1559,16 @@ def main():
...
@@ -1390,15 +1559,16 @@ def main():
#Q = readQ('./adc2019problem/Q005_10X10_b7_l6.txt')
#Q = readQ('./adc2019problem/Q005_10X10_b7_l6.txt')
#Q = readQ('./adc2019problem/Q006_10X10_b8_l9.txt')
#Q = readQ('./adc2019problem/Q006_10X10_b8_l9.txt')
#Q = readQ('./adc2019problem/Q007_10X10_b7_l8.txt')
#Q = readQ('./adc2019problem/Q007_10X10_b7_l8.txt')
#
Q = readQ('./adc2019problem/Q008_10X10_b10_l0.txt')
Q
=
readQ
(
'./adc2019problem/Q008_10X10_b10_l0.txt'
)
#Q = readQ('./adc2019problem/Q009_10X10_b7_l9.txt')
#Q = readQ('./adc2019problem/Q009_10X10_b7_l9.txt')
#Q = readQ('./adc2019problem/Q010_10X10_b20_l24.txt')
#Q = readQ('./adc2019problem/Q010_10X10_b20_l24.txt')
#Q = readQ('./adc2019problem/Q013_10X10_b8_l9.txt')
#Q = readQ('./adc2019problem/Q013_10X10_b8_l9.txt')
#Q = readQ('./adc2019problem/Q014_10X10_b9_l9.txt')
#Q = readQ('./adc2019problem/Q014_10X10_b9_l9.txt')
#Q = readQ('./adc2019problem/Q015_10X10_b8_l9.txt')
#Q = readQ('./adc2019problem/Q015_10X10_b8_l9.txt')
Q
=
readQ
(
'./adc2019problem/QRAND327_20X20_b20_l15.txt'
)
#
Q = readQ('./adc2019problem/QRAND327_20X20_b20_l15.txt')
#Q = readQ('./adc2019problem/QRAND368_10X10_b10_l15.txt')
#Q = readQ('./adc2019problem/QRAND368_10X10_b10_l15.txt')
#Q = readQ('./adc2019problem/random_problem/QRAND136_30X30_b30_l30.txt')
#Q = readQ('./adc2019problem/random_problem/QRAND136_30X30_b30_l30.txt')
#Q = readQ('./Q1.txt')
mode
=
1
mode
=
1
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment