-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueensProblemBeta.py
More file actions
189 lines (119 loc) · 4.59 KB
/
Copy pathQueensProblemBeta.py
File metadata and controls
189 lines (119 loc) · 4.59 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
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
179
180
181
182
183
184
185
186
187
188
189
class QueensProblem():
def __init__(self, numSquares):
self.board = []
self.num = numSquares
squares = numSquares
self.solutions = []
self.count = 0
while squares > 0:
tempsquares = numSquares
temp = []
while tempsquares > 0:
temp.append("*")
tempsquares -= 1
self.board.append(temp)
squares -= 1
def __str__(self):
answer = ""
count = 0
for i in self.solutions:
#print (i)
count += 1
answer += "Solution: " + str(count) + "\n" + "\n"
for j in i:
temp = ""
for q in j:
temp += str(q) + " "
answer += temp + "\n"
answer += "\n"
return answer
def isValidPlace(self, row, col):
valid = False
rowCount = 0
hasQ = False
#print("Testing for a row of:", row, "and column of:", col)
for i in range (len(self.board)):
colCount = 0
if "Q" in self.board[i]:
#print(i)
hasQ = True
if i == row:
return False
else:
for j in range (len(self.board[i])):
if self.board[i][j] == "Q":
if j == col:
return False
if hasQ:
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#South east check (+, +)
# print("South east check")
while diagRow < (len(self.board) - 1) and diagCol < (len(self.board) -1):
diagRow += 1
diagCol += 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
# print(diagRow, diagCol)
#North East Check (-, +)
# print("North East Check")
while diagRow > 0 and diagCol < (len(self.board) -1):
diagRow -= 1
diagCol += 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
# print(diagRow, diagCol)
#North West Check (-, -)
# print("North West Check")
while diagRow > 0 and diagCol > 0:
diagRow -= 1
diagCol -= 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#South West Check (+, -)
# print("South West Check")
while diagRow < (len(self.board) - 1) and diagCol > 0:
diagRow += 1
diagCol -= 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
#print("Success")
return True
def solve(self, col):
if col == self.num:
self.count +=1
#temp boards
temp = []
for i in range(len(self.board)):
temp.append(self.board[i][:])
self.solutions.append(temp)
return
for row in range(self.num):
if self.isValidPlace(row, col):
self.board[row][col] = "Q"
self.solve(col + 1)
self.board[row][col] = "*"
return self.count
def main():
squares = int(input("Input a number above 4: "))
while squares < 4:
print("Invalid input")
squares = int(input("Input a number above 4: "))
qProblem = QueensProblem(squares)
#print(qProblem)
print()
qProblem.solve(0)
print (qProblem)
main()