-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhalfPuzzle.py
More file actions
127 lines (107 loc) · 3.62 KB
/
halfPuzzle.py
File metadata and controls
127 lines (107 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from matrix import *
def reconstruct_image(m):
equal=True
use=set()
puzzle = Matrix.load("./puzzle/im"+str(find_left_top())+".bitmap")
for item in range(1,m*m+1):
for i in range(1,m*m+1):
if i in use:
continue
right_im = Matrix.load("./puzzle/im"+str(i)+".bitmap")
for row in range(19):
if puzzle[row,puzzle.dim()[1]-1]!=right_im[row,0]:
equal=False
if equal==True:
puzzle=join_h(puzzle,right_im)
use.add(i)
equal=True
return puzzle
def build_rows(m):
equal=True
use=set()
puzzle = Matrix.load("./puzzle/im"+str(find_left_top())+".bitmap")
for item in range(1,m*m+1):
for i in range(1,m*m+1):
if i in use:
continue
right_im = Matrix.load("./puzzle/im"+str(i)+".bitmap")
for row in range(19):
if puzzle[row,puzzle.dim()[1]-1]!=right_im[row,0]:
equal=False
if equal==True:
puzzle=join_h(puzzle,right_im)
use.add(i)
equal=True
return puzzle
def find_top(i):
x = Matrix.load("./puzzle/im"+str(i)+".bitmap")
for mat in range(1,401):
y = Matrix.load("./puzzle/im"+str(mat)+".bitmap")
cnt=0
for pix in range(25):
if (x[0,pix] == y[18,pix]):
cnt+=1
if cnt==25:
return False
return True
def find_left(i):
x = Matrix.load("./puzzle/im"+str(i)+".bitmap")
for mat in range(1,401):
y = Matrix.load("./puzzle/im"+str(mat)+".bitmap")
cnt=0
for pix in range(19):
if (x[pix,0] == y[pix,24]):
cnt+=1
if cnt==19:
return False
return True
def find_left_top():
for mat in range(1,401):
if find_top(mat) and find_left(mat):
return mat
def sort_rows(firstcol): #input: list with the first column indexes (using find_left)
newlist = [282]
for item in range(len(firstcol)):
x = Matrix.load("./puzzle/im"+str(newlist[item])+".bitmap")
for i in range(len(firstcol)):
y = Matrix.load("./puzzle/im"+str(firstcol[i])+".bitmap")
cnt=0
for pix in range(25):
if (x[18,pix] == y[0,pix]):
cnt+=1
if cnt==25:
newlist += [firstcol[i]]
return newlist
def join_v(mat1, mat2):
""" joins two matrices, side by side with some separation """
n1,m1 = mat1.dim()
n2,m2 = mat2.dim()
m = max(m1,m2)
n = n1+n2
new = Matrix(n, m, val=255) # fill new matrix with white pixels
for i in range(n1):
for j in range(m1):
new[i,j] = mat1[i,j]
for i in range(n2):
for j in range(m2):
new[i+n1,j] = mat2[i,j]
return new
def join_h(mat1, mat2):
""" joins two matrices, side by side with some separation """
n1,m1 = mat1.dim()
n2,m2 = mat2.dim()
m = m1+m2
n = max(n1,n2)
new = Matrix(n, m, val=255) # fill new matrix with white pixels
new[:n1,:m1] = mat1
new[:n2,m1:m] = mat2
'''
#without slicing:
for i in range(n1):
for j in range(m1):
new[i,j] = mat1[i,j]
for i in range(n2):
for j in range(m2):
new[i,j+m1+10] = mat2[i,j]
'''
return new