コード例 #1
0
    distances[s] = 0
    parents = [-1] * len(G)
    bfs(G, visited, distances, parents, s)
    dist = distances[t]
    if dist == inf:
        return None
    vertices = []
    i = t
    while parents[i] != -1:
        vertices.append((parents[i], i))
        i = parents[i]
    for vertex in vertices:
        G[vertex[0]].remove(vertex[1], )
        G[vertex[1]].remove(vertex[0], )
        for i in range(len(G)):
            visited[i] = False
            distances[i] = inf
            parents[i] = -1
        visited[s] = True
        distances[s] = 0
        bfs(G, visited, distances, parents, s)
        new_dist = distances[t]
        if new_dist > dist:
            return vertex
        G[vertex[0]].append(vertex[1])
        G[vertex[1]].append(vertex[0])
    return None


runtests(enlarge)
コード例 #2
0
# Labirynt. Labirynt reprezentowany jest przez tablicę w wierszy, z których każdy jest napisem
# składającym się z k kolumn. Pusty znak oznacza pole po którym robot może się poruszać, a znak ’X’
# oznacza ścianę labiryntu. Labirynt zawsze otoczony jest ścianami i nie da się opuścić planszy.
# Pozycja robota. Początkowo robot znajduje się na pozycji A = (x[a], y[a]) i jest obrócony w prawo
# (tj. znajduje się w wierszu y[a] i kolumnie x[a], skierowany w stronę rosnących numerów kolumn).
from Exercise_2_tests import runtests
from queue import PriorityQueue


def robot(L, A, B):
    DP = [[[[-1] * 3 for _ in range(4)] for _ in range(len(L[0]))] for _ in range(len(L))]
    queue = PriorityQueue()
    queue.put((0, A[0], A[1], 0, 0))
    possible_moves = [(1, 0), (0, 1), (-1, 0), (0, -1)]
    seconds = [60, 40, 30]
    while not queue.empty():
        time, x, y, direction, idx = queue.get()
        if (x, y) == B:
            return time
        if DP[y][x][direction][idx] != -1 or L[y][x] == 'X':
            continue
        DP[y][x][direction][idx] = time
        queue.put((time + 45, x, y, (direction + 1) % 4, 0))
        queue.put((time + 45, x, y, (direction + 3) % 4, 0))
        x += possible_moves[direction][0]
        y += possible_moves[direction][1]
        queue.put((time + seconds[idx], x, y, direction, min(idx + 1, 2)))


runtests(robot)
コード例 #3
0
                u = queue.get()
                for v in range(len(graph)):
                    if not visited[v] and graph[u][v] == 1:
                        queue.put(v)
                        visited[v] = True
                        result[v] = count
    return count


def breaking(G):
    to_remove = []
    max_breaking = 2
    breaking_vertex = None
    for i in range(len(G)):
        for j in range(len(G)):
            if G[i][j] != 0:
                to_remove.append(j)
        for k in range(len(to_remove)):
            G[i][to_remove[k]] = G[to_remove[k]][i] = 0
        actual_break = components(G)
        if actual_break > max_breaking:
            max_breaking = actual_break
            breaking_vertex = i
        for k in range(len(to_remove)):
            G[i][to_remove[k]] = G[to_remove[k]][i] = 1
        to_remove.clear()
    return breaking_vertex


runtests(breaking)
コード例 #4
0
# Rozwiązanie należy zaimplementować w postaci funkcji:
# def cutthetree(T):
#     ...
# która przyjmuje korzeń danego drzewa BST i zwraca wartość rozwiązania. Nie wolno zmieniać definicji
# class BNode.
from Exercise_2_tests import runtests
from math import inf


class BNode:
    def __init__(self, value):
        self.left = None
        self.right = None
        self.parent = None
        self.value = value


def cutthetree(T):
    if T is None:
        return 0
    if T.right is None and T.left is None:
        return inf
    actual_value = inf
    if T.parent is not None:
        actual_value = T.value
    actual_value = min(actual_value, cutthetree(T.right) + cutthetree(T.left))
    return actual_value


runtests(cutthetree)
コード例 #5
0
def count_sum_of_edges(T):
    for e in T.edges:
        count_sum_of_edges(e)
    for i in range(len(T.edges)):
        T.sum_of_edges += T.weights[i] + T.edges[i].sum_of_edges


def find_best_edge(root, node, best_index, best_diff):
    for i in range(len(node.edges)):
        actual_index, actual_diff = find_best_edge(root, node.edges[i],
                                                   best_index, best_diff)
        root_subtree = root.sum_of_edges - node.edges[
            i].sum_of_edges - node.weights[i]
        node_subtree = node.edges[i].sum_of_edges
        if actual_diff < best_diff:
            best_diff = actual_diff
            best_index = actual_index
        if abs(root_subtree - node_subtree) < best_diff:
            best_diff = abs(root_subtree - node_subtree)
            best_index = node.ids[i]
    return best_index, best_diff


def balance(T):
    count_sum_of_edges(T)
    best_index, best_diff = find_best_edge(T, T, None, inf)
    return best_index


runtests(balance)
コード例 #6
0
    is_sorted = None
    actual = head
    while actual is not None:
        nex_node = actual.next
        is_sorted = sorted_insert(is_sorted, actual)
        actual = nex_node
    head = is_sorted
    return head


def sorted_insert(head, new_node):
    if head is None or head.val >= new_node.val:
        new_node.next = head
        head = new_node
    else:
        actual = head
        while actual.next is not None and actual.next.val < new_node.val:
            actual = actual.next
        new_node.next = actual.next
        actual.next = new_node
    return head


def SortH(p, k):
    if k == 0:
        return p
    return insertion_sort(p)


runtests(SortH)
コード例 #7
0

def recursion(L, K, T, result, idx):
    if len(result) == len(T):
        return True
    for j in range(len(T)):
        if result[idx] % (10 ** K) == T[j][0] // (10 ** K) and not T[j][1]:
            result.append(T[j][0])
            T[j][1] = True
            recursion(L, K, T, result, idx + 1)
    if len(result) != len(T):
        idx -= 1
        value = result.pop()
        T[L.index(value)][1] = False


def order(L, K):
    start = min(L)
    T = [0] * len(L)
    for i in range(len(T)):
        if L[i] == start:
            T[i] = [L[i], True]
        else:
            T[i] = [L[i], False]
    result = [start]
    recursion(L, K, T, result, 0)
    return result


runtests(order)