forked from palsumitpal/sumitpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8QueensProblem.py
More file actions
242 lines (213 loc) · 6.2 KB
/
8QueensProblem.py
File metadata and controls
242 lines (213 loc) · 6.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import sys
GRIDSIZE = 8
threatMatrix = []
def resetMoveDictExceptTopRow():
for keys in moveDict.iterkeys():
if keys != 1:
moveDict[keys] = None
def printAt(row, col, ch):
if row > GRIDSIZE or col > GRIDSIZE:
return
if row <= 0 or col <= 0:
return
for i in range(1,GRIDSIZE+1):
for y in range(1,GRIDSIZE+1):
if ( i == row and y == col):
print ch + " ",
else:
print "- ",
print("") # new line
def printEmptyBoard():
for i in range(1,GRIDSIZE+1):
for y in range(1,GRIDSIZE+1):
print "- ",
print("") # new line
def getQueenMovesFrom(row, col):
validMoves = []
# print "getQueenMovesFrom " + str(row) + ", " + str(col)
for i in range(1,GRIDSIZE+1):
validMoves.append((i,col))
for i in range(1,GRIDSIZE+1):
validMoves.append((row,i))
#print "getQueenMovesFrom " + str(validMoves)
currentRow = row
currentCol = col
while currentRow != 1 and currentCol != 1:
validMoves.append((currentRow-1,currentCol-1))
currentRow -= 1
currentCol -= 1
# print "getQueenMovesFrom " + str(validMoves)
currentRow = row
currentCol = col
while currentRow < GRIDSIZE and currentCol < GRIDSIZE:
validMoves.append((currentRow+1,currentCol+1))
currentRow += 1
currentCol += 1
currentRow = row
currentCol = col
while currentRow < GRIDSIZE and currentCol != 1 :
validMoves.append((currentRow+1,currentCol-1))
currentRow += 1
currentCol -= 1
currentRow = row
currentCol = col
while currentRow != 1 and currentCol < GRIDSIZE :
validMoves.append((currentRow-1,currentCol+1))
currentRow -= 1
currentCol += 1
return validMoves
def showPossibleQueenMoves(row, col):
moves = getQueenMovesFrom(row, col)
for i in range(1,GRIDSIZE+1):
for y in range(1,GRIDSIZE+1):
if ( i == row and y == col):
print "Q ",
elif (i,y) in moves:
print "q ",
else :
print "- ",
print("") # new line
def getAllNonAttackingMoves():
threatened = []
def initializeThreatMatrix():
# l = []
# for j in range(1, GRIDSIZE+1):
# l.append(0)
#print l
#threatMatrix = []
for i in range(1, GRIDSIZE+1):
l = []
for j in range(1, GRIDSIZE+1):
l.append(0)
threatMatrix.append(l)
def printThreatMatrix():
for i in range(0, GRIDSIZE):
# for j in range(0, GRIDSIZE):
print str(threatMatrix[i])
print ""
def getPossibleMovesInRow(row):
rowThreats = threatMatrix[row-1]
rowMoves = []
for col in range(0,GRIDSIZE):
if threatMatrix[row-1][col] == 0:
rowMoves.append(col+1)
return rowMoves
def setThreatMatrix(row, col, v):
threatMatrix[row-1][col-1] = v
def updateThreatMatrix(listOfThreats, v, row, col):
updatedList = []
for threat in listOfThreats:
#print threat
r = threat[0]
c = threat[1]
if ( v == 1 ):
if ( threatMatrix[r-1][c-1] != 1 ):
threatMatrix[r-1][c-1] = v
updatedList.append((r,c))
else:
threatMatrix[r-1][c-1] = 0
#printThreatMatrix()
threatMatrix[row-1][col-1] = v
return updatedList
def resetAllMovesForRowsBelow(dict, rowCount):
# print "calling resetMovesForRowsBelow " + str(rowCount)
for i in range(rowCount+1,GRIDSIZE+1):
if dict.get(i) != None:
dict[i] = None
def allMovesTested():
for k in moveDict.keys():
if moveDict[k]==None:
continue
else:
return False
return True
def inMoves(r,c,moves):
for x,y in moves:
if x == r and y == c:
return True
return False
def printQueen(moves):
for x in range(1,GRIDSIZE+1):
for y in range(1,GRIDSIZE+1):
if ( inMoves(x,y,moves)):
print "Q ",
else:
print "- ",
print("") # new line
GRIDSIZE = int(sys.argv[1])
#print GRIDSIZE
#printEmptyBoard()
#print "------------"
moves = []
#for i in range(1,9):
## moves.append(len(getQueenMovesFrom(i, j)))
#print sorted(moves)
#showPossibleQueenMoves(1,1)
#print '------'
#showPossibleQueenMoves(2,3)
initializeThreatMatrix()
#printThreatMatrix()
#setThreatMatrix(0,0)
#setThreatMatrix(0,1)
#setThreatMatrix(0,2)
#print getPossibleMovesInRow(0)
moveDict = {}
rowCount = 1
updatedCoordinatesDict = {}
while True:
# while rowCount < GRIDSIZE+1 and numOfTries < GRIDSIZE*GRIDSIZE:
while rowCount < GRIDSIZE+1:
allowableMovesOnThisRow = moveDict.get(rowCount)
# print 'Before allowableMovesOnThisRow[' + str(rowCount) + "] = " + str(allowableMovesOnThisRow)
if allowableMovesOnThisRow == None:
allowableMovesOnThisRow = getPossibleMovesInRow(rowCount)
# print 'After allowableMovesOnThisRow[' + str(rowCount) + "] = " + str(allowableMovesOnThisRow)
if ( len(allowableMovesOnThisRow) != 0 ):
moveDict[rowCount] = allowableMovesOnThisRow
#print moveDict[rowCount]
currentMove = (rowCount,moveDict[rowCount].pop())
# print currentMove
currRow = rowCount
currCol = currentMove[1]
listOfThreats = getQueenMovesFrom(rowCount, currCol)
# print "List Of Threats " + str(listOfThreats)
#showPossibleQueenMoves(rowCount, currCol)
#printThreatMatrix()
#print moveDict[i]
if len(listOfThreats) > 0:
updatedCoordinates = updateThreatMatrix(listOfThreats, 1, rowCount, currCol)
updatedCoordinatesDict[currentMove] = updatedCoordinates
rowCount += 1
moves.append(currentMove)
# printThreatMatrix()
else:
moveDict[rowCount] = None
if (len(moves) != 0):
#print rowCount
lastMove = moves.pop()
lastMoveRow = lastMove[0]
lastMoveCol = lastMove[1]
resetAllMovesForRowsBelow(moveDict, rowCount)
rowCount -= 1
listOfThreatsToUndo = updatedCoordinatesDict.get(lastMove)
#moveDict[rowCount] =
updateThreatMatrix(listOfThreatsToUndo, 0, lastMoveRow, lastMoveCol)
# printThreatMatrix()
if (len(moves) == GRIDSIZE):
print "Possible Location = " + str(moves)
printQueen(moves)
if (allMovesTested()):
break
if (rowCount == GRIDSIZE+1):
if(len(moveDict[1]) != 0):
rowCount = 1
updatedCoordinatesDict = {}
threatMatrix = []
moves = []
initializeThreatMatrix()
resetMoveDictExceptTopRow()
continue
else:
break
else:
break