-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMatrix
178 lines (98 loc) · 3.71 KB
/
Matrix
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import numpy as np
from numpy.linalg import matrix_power
# Function to print the output
def printTheArray(arr, n):
for i in range(0, n):
print(arr[i], end = " ")
print()
# Function to generate all binary strings
def checkBlock(block):
correctList=list()
isCorrect=True;
for i in range(len(block)-1):
if block[i]==1 and block[i+1]==1:
isCorrect=False;
if isCorrect==True:
return True
# try making a list of them, initialize empty list
blocks = list()
def generateAllBinaryStrings(n, arr, i):
if i == n:
#printTheArray(arr, n)
piece=list()
for k in range(n):
piece.append(arr[k])
if checkBlock(piece):
blocks.append(piece)
return
# First assign "0" at ith position
# and try for all other permutations
# for remaining positions
arr[i] = 0
generateAllBinaryStrings(n, arr, i + 1)
# And then assign "1" at ith position
# and try for all other permutations
# for remaining positions
arr[i] = 1
generateAllBinaryStrings(n, arr, i + 1)
def createMatrix(blocks):
columns=np.zeros([len(blocks),len(blocks)], int)
#columns=len(blocks)*rows
columns = checkEdge(columns)
print("The adjacency matrix is:\n",columns,"\n\n")
numOfPerm(n,x,columns)
printBoard(columns)
def printBoard(columns):
loopIndex = 1
boardArray = np.empty((n,n))
i = np.random.randint(0,((2*n)-1))
boardArray[0] = blocks[i]
while loopIndex < n:
j = np.random.randint(0,((2*n)-1))
if i != j:
if columns[i][j] == 1:
boardArray[loopIndex] = blocks[j]
i = j
loopIndex += 1
print("\nExample Board layout:\n")
print(boardArray)
def checkEdge(columns):
m=len(blocks)
count=0
falseCount=0
isCompat=True
for i in range(m):
for j in range(m):
isCompat = True
for k in range(n):
if blocks[i][k]==1 and blocks[j][k]==1:
isCompat=False
if isCompat==True:
count += 1
columns[i][j]=1
else:
falseCount+=1
print("\nThe count is ",count,"\n")
print("The false count is ",falseCount,"\n")
matches=0
for i in range(n):
for j in range(n):
for k in range(n):
if blocks[i][k]==1 and blocks[j][k]==1:
matches+=1
print("The number of times they both are one is ",matches,"\n")
return columns
def numOfPerm(n,x,A):
raisedToPower = np.linalg.matrix_power(A,x-1)
print("The adjacency matrix to the power", x-1," is:\n",raisedToPower,"\n\n")
numberOfWays = np.sum(raisedToPower)
print("\nThe number of ways to arrange on a", n," by",x," board are", numberOfWays)
# Driver Code
if __name__ == "__main__":
n = int(input("Input an int n for the board row height:\n"))
x= int(input("\nInput an integer value for number of columns:\n"))
arr = [None] * n
# Print all binary strings
generateAllBinaryStrings(n, arr, 0)
createMatrix(blocks)
print("\nThe arrangement of blocks:\n",blocks)