Question: In python, need help finishing code. The program computes the shortest route one can take to catch all the unique Pokemon and then returns the
In python, need help finishing code.
The program computes the shortest route one can take to catch all the unique Pokemon and then returns the player back to the starting point. Each stop's location is indicated by two integers (r, c) where r is the number of blocks north of your starting point and c is the number of blocks west of your starting point.
For example, if the locations of five poke stops are (5,9), (20,20), (1,1), (1,8), (2,8) and the names are Evevee, Flareon, Flareon, Jolteon, Umbreon. So, the best route is to visit the first, fifth, fourth, and third stops in that order for a total distance of 28 blocks.
To compute distance: The distance from (0,0) to (5,9) is 14. The distance from (5,9) to (2,8) is 4. The distance from (2,8) to (1,8) is 1. The distance from (1,8) to (1,1) is 7. The distance from (1,1) to (0,0) is 2. 14 + 4 + 1 + 7 + 2 = 28
The input holds a single test case. The test case begins with a single integer, which indicates the number of poke stops to consider. Each next n lines specifies the location of a pokemon stop and the name of the Pokemon. The location is two integers (r, c) of the location. The location is followed by a single space and the Pokemon's name. The number of unique Pokemon is always less than or equal to 15.
The output gives the shortest distance, in blocks, required to catch all the unique Pokemon.
Sample Input: 5 5 9 Eevee 10 10 Flareon 1 1 Flareon 1 8 Jolteon 2 8 Umbreon
Sample Output: 28
"""
class PokeStop:
def __init__(self, r, c, n, id): self.row = r self.col = c self.name = n self.ID = id
def __str__(self): return "%s %s %s %s" % (self.row, self.col, self.name, self.ID)
class PokeNode:
def __init__(self, p, d, b): self.path = p self.dist = d self.bound = b
def __str__(self): return "%s %s %s" % (self.path, self.dist, self.bound)
def shortDist(stops, distSoFar, listSoFar): #Need help finishing this function and the main to get the program to work adjMatrix = makeAdjMatrix(stops) minList = minDist(adjMatrix) bound = computeBound(listSoFar, distSoFar, minList) node = PokeNode(listSoFar, distSoFar, bound)
"""need popped node and for each i not in poppedNode, listSoFar
and where i does not have repeated pokemon, make a new node
newNodePath = poppednode.path.append(i)
newNodeDist = poppedNode.distance + distance between last 2 elements in the path
compute a new bound for new Node
push new Node in the priority queue"""
def removeItem(heap, index): while index > 0: up = (index +1) / 2 -1 heap[index] = heap[up] index = up heapq.heappop(heap)
def computeBound(listSoFar, distSoFar, minList): total = distSoFar for i in range(len(minList)): if i not in listSoFar: total += minList[i] return total
def getDistance(Poke1, Poke2): return abs((Poke2.row - Poke1.row)) + abs((Poke2.col - Poke1.col))
def makeAdjMatrix(stops): m = [] for i in stops: rowList = [] for j in stops: rowList.append(getDistance(i, j)) m.append(rowList) return m
def minDist(adjMatrix): minList = [] for list in adjMatrix: list = list.sort() minList.append(list[1]) return minList
numberStops = int(input()) stops = [PokeStop(0, 0, " ", 0)]
for i in range(numberStops): row, column, name = input().split() stops.append(PokeStop(int(row), int(column), name, i + 1))
for p in stops: print(p)
adjMatrix = makeAdjMatrix(stops) distance = shortDist(stops, 0, [stops[0]])
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
