Commit c46fbaaebb49f83aa16b9954281b1707b8a3b0a3
1 parent
3449138d
Finished 18
Showing
1 changed file
with
11 additions
and
22 deletions
18.py
1 | #!/usr/bin/python | 1 | #!/usr/bin/python |
2 | 2 | ||
3 | import sys | 3 | import sys |
4 | -import pdb | ||
5 | 4 | ||
6 | class Node: | 5 | class Node: |
7 | - parentNodes = [] | ||
8 | - childNodes = [] | ||
9 | - weight = 0 | ||
10 | - value = 0 | ||
11 | 6 | ||
12 | def __init__(self,value): | 7 | def __init__(self,value): |
8 | + self.parentNodes = [] | ||
9 | + self.childNodes = [] | ||
10 | + self.weight = 0 | ||
13 | self.value = value | 11 | self.value = value |
14 | 12 | ||
15 | def calcWeight(self): | 13 | def calcWeight(self): |
@@ -31,21 +29,20 @@ nodeMap = [] | @@ -31,21 +29,20 @@ nodeMap = [] | ||
31 | def buildNodeMap(nodeMap): | 29 | def buildNodeMap(nodeMap): |
32 | for i in range(0,len(nodeMap)): | 30 | for i in range(0,len(nodeMap)): |
33 | for j in range(0,len(nodeMap[i])): | 31 | for j in range(0,len(nodeMap[i])): |
34 | - node = nodeMap[i][j] | 32 | + currentNode = nodeMap[i][j] |
35 | 33 | ||
36 | if(i != len(nodeMap)-1): | 34 | if(i != len(nodeMap)-1): |
37 | - node.childNodes.append(nodeMap[i+1][j]) | ||
38 | - node.childNodes.append(nodeMap[i+1][j+1]) | ||
39 | - | 35 | + currentNode.childNodes.append(nodeMap[i+1][j]) |
36 | + currentNode.childNodes.append(nodeMap[i+1][j+1]) | ||
40 | if(i != 0): | 37 | if(i != 0): |
41 | if(j == 0): | 38 | if(j == 0): |
42 | - node.parentNodes.append(nodeMap[i-1][j]) | 39 | + currentNode.parentNodes.append(nodeMap[i-1][j]) |
43 | elif(j == len(nodeMap[i])-1): | 40 | elif(j == len(nodeMap[i])-1): |
44 | - node.parentNodes.append(nodeMap[i-1][j-1]) | 41 | + currentNode.parentNodes.append(nodeMap[i-1][j-1]) |
45 | else: | 42 | else: |
46 | - node.parentNodes.append(nodeMap[i-1][j]) | ||
47 | - node.parentNodes.append(nodeMap[i-1][j-1]) | ||
48 | - | 43 | + currentNode.parentNodes.append(nodeMap[i-1][j]) |
44 | + currentNode.parentNodes.append(nodeMap[i-1][j-1]) | ||
45 | + | ||
49 | 46 | ||
50 | def dijkstraReverse(nodeMap): | 47 | def dijkstraReverse(nodeMap): |
51 | for row in nodeMap: | 48 | for row in nodeMap: |
@@ -59,8 +56,6 @@ for line in sys.stdin: | @@ -59,8 +56,6 @@ for line in sys.stdin: | ||
59 | nodeMap[numRow].append(Node(int(elem))) | 56 | nodeMap[numRow].append(Node(int(elem))) |
60 | numRow += 1 | 57 | numRow += 1 |
61 | 58 | ||
62 | -pdb.set_trace() | ||
63 | - | ||
64 | buildNodeMap(nodeMap) | 59 | buildNodeMap(nodeMap) |
65 | nodeMap.append([]) | 60 | nodeMap.append([]) |
66 | targetNode = Node(0) | 61 | targetNode = Node(0) |
@@ -69,12 +64,6 @@ for node in nodeMap[-2]: | @@ -69,12 +64,6 @@ for node in nodeMap[-2]: | ||
69 | 64 | ||
70 | nodeMap[-1].append(targetNode) | 65 | nodeMap[-1].append(targetNode) |
71 | 66 | ||
72 | -for row in nodeMap: | ||
73 | - string = "" | ||
74 | - for node in row: | ||
75 | - string += str(node) + " " | ||
76 | - print string | ||
77 | - | ||
78 | dijkstraReverse(nodeMap) | 67 | dijkstraReverse(nodeMap) |
79 | 68 | ||
80 | print "Result is: " + str(targetNode.weight) | 69 | print "Result is: " + str(targetNode.weight) |