Commit 3449138d38a0ce49b327704a175db1908529a8d7
1 parent
28c8dda0
Set 17 to executable. Added problem 18
Showing
3 changed files
with
95 additions
and
0 deletions
17.py
100644 โ 100755
18.data
0 โ 100644
1 | +75 | ||
2 | +95 64 | ||
3 | +17 47 82 | ||
4 | +18 35 87 10 | ||
5 | +20 04 82 47 65 | ||
6 | +19 01 23 75 03 34 | ||
7 | +88 02 77 73 07 63 67 | ||
8 | +99 65 04 28 06 16 70 92 | ||
9 | +41 41 26 56 83 40 80 70 33 | ||
10 | +41 48 72 33 47 32 37 16 94 29 | ||
11 | +53 71 44 65 25 43 91 52 97 51 14 | ||
12 | +70 11 33 28 77 73 17 78 39 68 17 57 | ||
13 | +91 71 52 38 17 14 91 43 58 50 27 29 48 | ||
14 | +63 66 04 68 89 53 67 30 73 16 69 87 40 31 | ||
15 | +04 62 98 27 23 09 70 98 73 93 38 53 60 04 23 |
18.py
0 โ 100755
1 | +#!/usr/bin/python | ||
2 | + | ||
3 | +import sys | ||
4 | +import pdb | ||
5 | + | ||
6 | +class Node: | ||
7 | + parentNodes = [] | ||
8 | + childNodes = [] | ||
9 | + weight = 0 | ||
10 | + value = 0 | ||
11 | + | ||
12 | + def __init__(self,value): | ||
13 | + self.value = value | ||
14 | + | ||
15 | + def calcWeight(self): | ||
16 | + maxWeight = 0 | ||
17 | + for node in self.parentNodes: | ||
18 | + weight = node.weight + node.value | ||
19 | + if(weight > maxWeight): | ||
20 | + maxWeight = weight | ||
21 | + self.weight = maxWeight | ||
22 | + | ||
23 | + def __str__(self): | ||
24 | + return str(self.value) | ||
25 | + | ||
26 | + | ||
27 | +targetNode = None | ||
28 | + | ||
29 | +nodeMap = [] | ||
30 | + | ||
31 | +def buildNodeMap(nodeMap): | ||
32 | + for i in range(0,len(nodeMap)): | ||
33 | + for j in range(0,len(nodeMap[i])): | ||
34 | + node = nodeMap[i][j] | ||
35 | + | ||
36 | + if(i != len(nodeMap)-1): | ||
37 | + node.childNodes.append(nodeMap[i+1][j]) | ||
38 | + node.childNodes.append(nodeMap[i+1][j+1]) | ||
39 | + | ||
40 | + if(i != 0): | ||
41 | + if(j == 0): | ||
42 | + node.parentNodes.append(nodeMap[i-1][j]) | ||
43 | + elif(j == len(nodeMap[i])-1): | ||
44 | + node.parentNodes.append(nodeMap[i-1][j-1]) | ||
45 | + else: | ||
46 | + node.parentNodes.append(nodeMap[i-1][j]) | ||
47 | + node.parentNodes.append(nodeMap[i-1][j-1]) | ||
48 | + | ||
49 | + | ||
50 | +def dijkstraReverse(nodeMap): | ||
51 | + for row in nodeMap: | ||
52 | + for node in row: | ||
53 | + node.calcWeight() | ||
54 | + | ||
55 | +numRow = 0 | ||
56 | +for line in sys.stdin: | ||
57 | + nodeMap.append([]) | ||
58 | + for elem in line.split(): | ||
59 | + nodeMap[numRow].append(Node(int(elem))) | ||
60 | + numRow += 1 | ||
61 | + | ||
62 | +pdb.set_trace() | ||
63 | + | ||
64 | +buildNodeMap(nodeMap) | ||
65 | +nodeMap.append([]) | ||
66 | +targetNode = Node(0) | ||
67 | +for node in nodeMap[-2]: | ||
68 | + targetNode.parentNodes.append(node) | ||
69 | + | ||
70 | +nodeMap[-1].append(targetNode) | ||
71 | + | ||
72 | +for row in nodeMap: | ||
73 | + string = "" | ||
74 | + for node in row: | ||
75 | + string += str(node) + " " | ||
76 | + print string | ||
77 | + | ||
78 | +dijkstraReverse(nodeMap) | ||
79 | + | ||
80 | +print "Result is: " + str(targetNode.weight) |