הפעם כתבנו תוכנה לפתרון פאזל נחמד, דומה לקטמינו, רק שהחלקים דמויי הטטריס מחוררים עם מקום לפינים שיכולים להיכנס לתוכם. בחידה מקבלים מראש את מיקום הפינים הצבעוניים, וצריך לסדר את כל החלקים, באופן שבתוך כל חלק יהיה אך ורק פין תואם בצבע (בתוך חלק אדום רק פין בצבע אדום ולא באף צבע אחר). חלקים יכולים להיות גם ריקים.
והנה הקוד:
import time
ct = time.time()
import copy
import matplotlib.pyplot as plt
z=[]
"""
red =10 green=30
yellow=20 blue=40 empty pin=minus
"""
Board=[[0,0,0,0,0,0,0,0],
[0,0,0,0,-40,0,0,0],
[0,0,0,0,0,-10,-30,0],
[0,0,-20,0,0,0,0,-30]]
#shapes:
R=[[10,1,0],[1,0,0],[10,0,0]]
P=[[4,4,4],[0,40,40],[0,0,0]]
Z=[[0,1,0],[1,10,0],[1,0,0]]
I=[[20,0,0],[2,0,0],[2,0,0]]
L=[[4,0,0,0],[40,0,0,0],[4,0,0,0],[4,0,0,0]]
V=[[3,30,0],[0,30,0],[0,0,0]]
X=[[0,20,0],[2,2,20],[0,0,20]]
T=[[3,3,30],[0,30,0],[0,0,0]]
def rotateMatrix(matx):
N=len(matx[0])
mat = copy.deepcopy(matx)
# Consider all squares one by one
for x in range(0, 2):
# Consider elements in group
# of 4 in current square
for y in range(x, N - x - 1):
# store current cell in temp variable
temp = mat[x][y]
# move values from right to top
mat[x][y] = mat[y][N - 1 - x]
# move values from bottom to right
mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y]
# move values from left to bottom
mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x]
# assign temp to left
mat[N - 1 - y][x] = temp
return mat
def allShapeForms(shape):
s = []
s.append(shape)
cp = copy.deepcopy(shape)
for i in range(3):
x = rotateMatrix(cp)
s.append(x)
cp = x
cp = []
for item in shape:
cp.append(item[::-1])
s.append(cp)
for i in range(3):
x = rotateMatrix(cp)
s.append(x)
cp = x
return s
def fitForm2Board (form,tempboard):
board=copy.deepcopy(tempboard)
combinationFormList=[]
collision = 0
for y in range(4):
for x in range (8):
for index, row in enumerate(form):
for ind, cell in enumerate(row):
if cell != 0:
if (index+y<4 and ind+x<8) and (board[index+y][ind+x] == 0 or board[index+y][ind+x] == -cell):
board[index+y][ind+x] = cell
else:
collision += 1
if collision > 0:
collision = 0
board = copy.deepcopy(tempboard)
else:
combinationFormList.append(board)
board =copy.deepcopy(tempboard)
return combinationFormList
def print2d(mat):
for item in mat:
print(item)
#return all bords:
def allShapePossibilities (shape,board):
allposs=[]
for item in allShapeForms(shape):
for b1 in fitForm2Board(item, board):
allposs.append(b1)
return allposs
for b1 in allShapePossibilities(X,Board):
for b2 in allShapePossibilities(I,b1):
for b3 in allShapePossibilities(P,b2):
for b4 in allShapePossibilities(L,b3):
for b5 in allShapePossibilities(R,b4):
for b6 in allShapePossibilities(Z,b5):
for b7 in allShapePossibilities(T,b6):
for b8 in allShapePossibilities(V,b7):
print2d(b8)
print(time.time()-ct)
z=b8
for ind, row in enumerate (z):
for index, num in enumerate (row):
if num==10:
z[ind][index]=10
if num==1:
z[ind][index]=12
if num==20:
z[ind][index]=40
if num==2:
z[ind][index]=42
if num==30:
z[ind][index]=30
if num==3:
z[ind][index]=32
if num==40:
z[ind][index] =20
if num==4:
z[ind][index]=22
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(z, extent=[1, 8, 1, 4], interpolation='none')
plt.show()
quit()
"""
#back traking loop
for item in allShapeForms(X):
for b1 in fitForm2Board(item, Board):
for item1 in allShapeForms(Z):
for b2 in fitForm2Board(item1, b1):
for item2 in allShapeForms(P):
for b3 in fitForm2Board(item2, b2):
for item3 in allShapeForms(R):
for b4 in fitForm2Board(item3, b3):
for item4 in allShapeForms(T):
for b5 in fitForm2Board(item4, b4):
for item5 in allShapeForms(L):
for b6 in fitForm2Board(item5, b5):
for item6 in allShapeForms(I):
for b7 in fitForm2Board(item6, b6):
for item7 in allShapeForms(V):
for b8 in fitForm2Board(item7, b7):
print2d(b8)
print("the end")
quit()
"""
so cool!
https://depositphotos.com/vector/vector-2021-06-25-sitemap-5025.xml
https://depositphotos.com/vector/vector-2021-06-24-sitemap-5026.xml
https://depositphotos.com/vector/vector-2021-06-23-sitemap-5027.xml
https://depositphotos.com/vector/vector-2021-06-26-sitemap-5028.xml
https://depositphotos.com/vector/vector-2021-06-25-sitemap-5029.xml
https://depositphotos.com/vector/vector-2021-06-30-sitemap-5030.xml
https://depositphotos.com/vector/vector-2021-06-25-sitemap-5031.xml
https://depositphotos.com/vector/vector-2021-06-27-sitemap-5032.xml
https://depositphotos.com/vector/vector-2021-06-30-sitemap-5033.xml
https://depositphotos.com/vector/vector-2021-06-30-sitemap-5034.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5035.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5036.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5037.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5038.xml
https://depositphotos.com/vector/vector-2021-06-30-sitemap-5039.xml
https://depositphotos.com/vector/vector-2021-06-30-sitemap-5040.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5041.xml
https://depositphotos.com/vector/vector-2021-07-01-sitemap-5042.xml
https://depositphotos.com/vector/vector-2021-07-04-sitemap-5043.xml
https://depositphotos.com/vector/vector-2021-07-04-sitemap-5044.xml
קוד משופר לראות את הצורות ברור יותר:
import time ct = time.time() import copy import matplotlib.pyplot as plt z=[] """ red =10 green=30 yellow=20 blue=40 empty pin=minus """ Board=[[0,-40,0,0,0,0,0,0], [-10,-20,0,0,0,0,0,0], [0,0,0,0,-20,-30,0,0], [0,0,0,0,0,-30,-40,0]] #shapes: R=[[10,1,0],[1,0,0],[10,0,0]] P=[[6,6,6],[0,40,40],[0,0,0]] Z=[[0,2,0],[2,11,0],[2,0,0]] I=[[20,0,0],[3,0,0],[3,0,0]] L=[[5,0,0,0],[41,0,0,0],[5,0,0,0],[5,0,0,0]] V=[[7,30,0],[0,30,0],[0,0,0]] X=[[0,21,0],[4,4,21],[0,0,21]] T=[[8,8,31],[0,31,0],[0,0,0]] def rotateMatrix(matx): N=len(matx[0]) mat = copy.deepcopy(matx) # Consider all squares one by one for x in range(0, 2): # Consider elements in group # of 4 in current square for y in range(x, N - x - 1): # store current cell in temp variable temp = mat[x][y] # move values from right to top mat[x][y] = mat[y][N - 1 - x] # move values from bottom to right mat[y][N - 1 - x] = mat[N - 1 - x][N - 1 - y] # move values from left to bottom mat[N - 1 - x][N - 1 - y] = mat[N - 1 - y][x] # assign temp to left mat[N - 1 - y][x] = temp return mat def allShapeForms(shape): s = [] s.append(shape) cp = copy.deepcopy(shape) for i in range(3): x = rotateMatrix(cp) s.append(x) cp = x cp = [] for item in shape: cp.append(item[::-1]) s.append(cp) for i in range(3): x = rotateMatrix(cp) s.append(x) cp = x return s def fitForm2Board (form,tempboard): board=copy.deepcopy(tempboard) combinationFormList=[] collision = 0 for y in range(4): for x in range (8): for index, row in enumerate(form): for ind, cell in enumerate(row): if cell != 0: if (index+y<4 and ind+x<8) and (board[index+y][ind+x] == 0 or board[index+y][ind+x] == -cell or (board[index+y][ind+x]-1) == -cell): board[index+y][ind+x] = cell else: collision += 1 if collision > 0: collision = 0 board = copy.deepcopy(tempboard) else: combinationFormList.append(board) board =copy.deepcopy(tempboard) return combinationFormList def print2d(mat): for item in mat: print(item) #return all bords: def allShapePossibilities (shape,board): allposs=[] for item in allShapeForms(shape): for b1 in fitForm2Board(item, board): allposs.append(b1) return allposs for b1 in allShapePossibilities(X,Board): for b2 in allShapePossibilities(I,b1): for b3 in allShapePossibilities(P,b2): for b4 in allShapePossibilities(L,b3): for b5 in allShapePossibilities(R,b4): for b6 in allShapePossibilities(Z,b5): for b7 in allShapePossibilities(T,b6): for b8 in allShapePossibilities(V,b7): print2d(b8) print(time.time()-ct) z=b8 for ind, row in enumerate (z): for index, num in enumerate (row): if num==10: z[ind][index]=20 if num==1: z[ind][index]=21 if num==11: z[ind][index]=30 if num==2: z[ind][index]=31 if num==20: z[ind][index]=60 if num==3: z[ind][index]=61 if num==21: z[ind][index]=70 if num==4: z[ind][index]=71 if num==30: z[ind][index]=40 if num==7: z[ind][index]=41 if num==31: z[ind][index]=10 if num==8: z[ind][index]=11 if num==40: z[ind][index]=30 if num==6: z[ind][index]=31 if num==41: z[ind][index]=50 if num==5: z[ind][index]=51 fig = plt.figure() ax = fig.add_subplot(111) ax.imshow(z, extent=[1, 8, 1, 4], interpolation='none') plt.show() quit()