-
Notifications
You must be signed in to change notification settings - Fork 3
/
KnightTourAgain.py
171 lines (157 loc) · 4.46 KB
/
KnightTourAgain.py
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
import sys
SIZE = 8
def printKnightAt(row, col):
if row > SIZE or col > SIZE:
return
if row <= 0 or col <= 0:
return
for i in range(1,SIZE+1):
for y in range(1,SIZE+1):
if ( i == row and y == col):
print "K ",
else:
print "- ",
print("") # new line
def printEmptyBoard():
for i in range(1,SIZE+1):
for y in range(1,SIZE+1):
print "- ",
print("") # new line
def getKnightMovesFrom(row, col):
validMoves = []
if ( row + 2 <= SIZE ):
#row+2, col+1
if ( col + 1 <= SIZE ):
tup = (row+2, col+1)
validMoves.append(tup)
#row+2, col-1
if ( col - 1 >= 1 ):
tup = (row+2, col-1)
validMoves.append(tup)
if ( row - 2 >= 1 ):
#row-2, col+1
if ( col + 1 <= SIZE ):
tup = (row-2, col+1)
validMoves.append(tup)
#row-2, col-1
if ( col - 1 >= 1 ):
tup = (row-2, col-1)
validMoves.append(tup)
if ( row + 1 <= SIZE ):
#row+1, col+2
if ( col + 2 <= SIZE ):
tup = (row+1, col+2)
validMoves.append(tup)
#row+1, col-2
if ( col - 2 >= 1 ):
tup = (row+1, col-2)
validMoves.append(tup)
if ( row - 1 >= 1 ):
#row-1, col+2
if ( col + 2 <= SIZE ):
tup = (row-1, col+2)
validMoves.append(tup)
#row-1, col-2
if ( col - 2 >= 1 ):
tup = (row-1, col-2)
validMoves.append(tup)
return validMoves
def getKnightMovesFromIfNotVisited(visitedList, row, col):
# print "Calling getMovesIfNotVisited For : " + str(row) + ", " + str(col)
validMoves = getKnightMovesFrom(row, col)
# print validMoves
newValidMoves = []
# print "ValidMoves in getMovesIfNotVisited : " + str(validMoves)
for currentMove in validMoves:
# print "Current location = " + str(currentMove)
# printArray(visitedArr)
# print ""
if ( currentMove not in visitedList ):
currentRow = currentMove[0]
currentCol = currentMove[1]
newValidMoves.append(currentMove)
return newValidMoves
def showPossibleKnightMoves(row, col):
moves = getKnightMovesFrom(row, col)
print repr(moves)
for i in range(1,SIZE+1):
for y in range(1,SIZE+1):
if ( i == row and y == col):
print "K ",
elif (i,y) in moves:
print "k ",
else :
print "- ",
print("") # new line
def doKnightTour(row, col):
listOfMoves = {}
tour = []
moves = getKnightMovesFromIfNotVisited(listOfMoves.keys(), row, col)
currentRow = row
currentCol = col
if len(moves) != 0:
listOfMoves[(row, col)] = moves
tour.append((currentRow, currentCol))
while len(tour) != SIZE*SIZE:
print "currentRow, currentCol : " + str(currentRow) + ", " + str(currentCol) + " Moves = " + str(listOfMoves[(currentRow, currentCol)])
if len(listOfMoves[(currentRow, currentCol)]) != 0:
while len(listOfMoves[(currentRow, currentCol)]) > 0:
# print len(listOfMoves[(currentRow, currentCol)])
tryingCurrent = listOfMoves[(currentRow, currentCol)].pop()
# print len(listOfMoves[(currentRow, currentCol)])
tryingCurrentRow, tryingCurrentCol = tryingCurrent[0], tryingCurrent[1]
moves = getKnightMovesFromIfNotVisited(listOfMoves.keys(), tryingCurrentRow, tryingCurrentCol)
print "Moves from tryingCurrentRow, tryingCurrentCol : " + str(tryingCurrentRow) + ", " + str(tryingCurrentCol) + " = " + str(moves)
if len(moves) != 0:
listOfMoves[(tryingCurrentRow, tryingCurrentCol)] = moves
currentRow = tryingCurrentRow
currentCol = tryingCurrentCol
tour.append((currentRow, currentCol))
break;
else:
if(len(tour) == SIZE*SIZE-1) and (tryingCurrent not in tour):
tour.append(tryingCurrent)
print "Found Tour : " + str(tour)
return tour;
else:
del listOfMoves[(currentRow, currentCol)]
#tour.remove((currentRow, currentCol))
print "Tour Length = " + str(len(tour))
print "Tour = " + str(tour)
tour.pop()
if ( len(tour) > 0 ):
nextMove = tour[-1] # last one
print "nextMove" + str(nextMove)
currentRow, currentCol = nextMove[0], nextMove[1]
else:
print "No Tour Found"
break
#print tour
def printArray(a):
for x in range(len(a)):
print a[x]
def isVisited(ar, r, c):
if ar[r][c] == 1:
return True
return False
def isAllVisited(arr):
for i in range(0,SIZE):
for j in range(0,SIZE):
if arr[i][i] != 1:
return False
return True
#printEmptyBoard()
print "------------"
#printKnightAt(2,3)
#showPossibleKnightMoves(2,7)
#showPossibleKnightMoves(6,7)
#showPossibleKnightMoves(7,6)
SIZE = int(sys.argv[1])
startRow = int(sys.argv[2])
startCol = int(sys.argv[3])
tour = doKnightTour(startRow,startCol)
if tour != None:
for (row,col) in tour:
printKnightAt(row,col)
print
print