-
Notifications
You must be signed in to change notification settings - Fork 0
/
myHamilton.py
66 lines (58 loc) · 2.91 KB
/
myHamilton.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
class MyHamilton():
def __init__(self):
self.graph = []
self.path = []
def makeHamiltonPath(self, rows, cols, matrix):
if(rows%2 == 0 or cols%2 == 0):
self.path.append(0)
if(rows%2 != 0):
dir = "down"
currentColumn = 0
for number in range(cols):
if(dir == "down"):
for i in range(1,rows):
self.path.append(matrix[i][currentColumn].nodeNumber)
if(i == rows-1):
self.path.append(self.path[len(self.path)-1]+1)
currentColumn += 1
dir = "up"
elif(dir == "up"):
if(self.path[len(self.path)-1] != rows*cols-1):
for i in range(rows-1,0,-1):
self.path.append(matrix[i][currentColumn].nodeNumber)
if(i == 1):
self.path.append(self.path[len(self.path) - 1] + 1)
currentColumn += 1
dir = "down"
else:
for i in range(rows-1,-1,-1):
self.path.append(matrix[i][currentColumn].nodeNumber)
for j in range (cols-1,0,-1):
self.path.append(matrix[0][j].nodeNumber)
elif (cols % 2 != 0):
dir = "right"
currentRow = 0
for number in range(rows):
if (dir == "right"):
for i in range(1, cols):
self.path.append(matrix[currentRow][i].nodeNumber)
if (i == cols - 1):
self.path.append(self.path[len(self.path) - 1] + cols)
currentRow += 1
dir = "left"
elif (dir == "left"):
if (self.path[len(self.path) - 1] != rows * cols - 1):
for i in range(cols - 1, 0, -1):
self.path.append(matrix[currentRow][i].nodeNumber)
if (i == 1):
self.path.append(self.path[len(self.path) - 1] + cols)
currentRow += 1
dir = "right"
else:
for i in range(cols - 1, -1, -1):
self.path.append(matrix[currentRow][i].nodeNumber)
for j in range(cols-1, 0,-1):
self.path.append(matrix[j][0].nodeNumber)
#print(self.path)
else:
print("No hamilton path found")