Example #1
0
    def timeToDeliver(self, path_str):
        start, end, graph = self.create_graph(path_str)           

        path = shortest_path(graph, start, end)
        dist = shortest_distance(graph, start, end)
        instructions = self.get_min_moves(path)
        cost = self.get_cost(instructions)

        return path, dist, instructions, cost
Example #2
0
    def timeToDeliver(self, path_str):
        start, end, graph = self.create_graph(path_str)

        path = shortest_path(graph, start, end)
        dist = shortest_distance(graph, start, end)
        instructions = self.get_min_moves(path)
        cost = self.get_cost(instructions)

        return path, dist, instructions, cost
Example #3
0
    def lowest(self, harmful_areas, deadly_areas):
        
        matrix = list()
        for row in range(0, size):
            row = list()
            matrix.append(row)

            for col in range(0, size):
                row.append(safe)
        
        for h in harmful_areas:
            x1, y1, x2, y2 = [int(n) for n in h.split()]
            for i in range(min(x1, x2), max(x1, x2) + 1):
                for j in range(min(y1, y2), max(y1, y2) + 1):
                    matrix[i][j] = harmful

        for d in deadly_areas:
            x1, y1, x2, y2 = [int(n) for n in d.split()]
            for i in range(min(x1, x2), max(x1, x2) + 1):
                for j in range(min(y1, y2), max(y1, y2) + 1):
                    matrix[i][j] = deadly 

        graph = {}
        for i in range(0, size):
            for j in range(0, size):

                state = matrix[i][j]
                
                if (state == deadly and not (i == 0 and j == 0)):
                    continue

                node = (i, j)
                neighbours = list()

                if i > 0 and matrix[i - 1][j] != deadly :
                    neighbours.append(((i - 1, j), matrix[i - 1][j]))
                
                if i < size - 1  and matrix[i + 1][j] != deadly :
                    neighbours.append(((i + 1, j), matrix[i + 1][j]))
 
                if j > 0 and matrix[i][j - 1] != deadly :
                    neighbours.append(((i, j - 1), matrix[i][j - 1]))

                if j < size - 1 and matrix[i][j + 1] != deadly :
                    neighbours.append(((i, j + 1), matrix[i][j + 1]))

                graph[node] = neighbours

        matrix = None 

        min_path = shortest_path(graph, (0, 0), (size - 1, size - 1))
        min_dist = shortest_distance(graph, (0, 0), (size - 1, size - 1))

        return min_dist