Blame view

18.py 1.4 KB
1
2
3
4
5
6
7
#!/usr/bin/python

import sys

class Node:

	def __init__(self,value):
Imanol-Mikel Barba Sabariego authored
8
9
10
		self.parentNodes = []
		self.childNodes = []
		self.weight = 0
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
		self.value = value

	def calcWeight(self):
		maxWeight = 0
		for node in self.parentNodes:
			weight = node.weight + node.value
			if(weight > maxWeight):
				maxWeight = weight
		self.weight = maxWeight

	def __str__(self):
		return str(self.value)


targetNode = None

nodeMap = []

def buildNodeMap(nodeMap):
	for i in range(0,len(nodeMap)):
		for j in range(0,len(nodeMap[i])):
Imanol-Mikel Barba Sabariego authored
32
			currentNode = nodeMap[i][j]
33
34

			if(i != len(nodeMap)-1):
Imanol-Mikel Barba Sabariego authored
35
36
				currentNode.childNodes.append(nodeMap[i+1][j])
				currentNode.childNodes.append(nodeMap[i+1][j+1])
37
38
			if(i != 0):
				if(j == 0):
Imanol-Mikel Barba Sabariego authored
39
					currentNode.parentNodes.append(nodeMap[i-1][j])
40
				elif(j == len(nodeMap[i])-1):
Imanol-Mikel Barba Sabariego authored
41
					currentNode.parentNodes.append(nodeMap[i-1][j-1])
42
				else:
Imanol-Mikel Barba Sabariego authored
43
44
45
					currentNode.parentNodes.append(nodeMap[i-1][j])
					currentNode.parentNodes.append(nodeMap[i-1][j-1])
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

def dijkstraReverse(nodeMap):
	for row in nodeMap:
		for node in row:
			node.calcWeight()

numRow = 0
for line in sys.stdin:
	nodeMap.append([])
	for elem in line.split():
		nodeMap[numRow].append(Node(int(elem)))
	numRow += 1

buildNodeMap(nodeMap)
nodeMap.append([])
targetNode = Node(0)
for node in nodeMap[-2]:
	targetNode.parentNodes.append(node)

nodeMap[-1].append(targetNode)

dijkstraReverse(nodeMap)

print "Result is: " + str(targetNode.weight)