Esempio n. 1
0
def eligibility_trace(nasheq_dict, child_partition, gamma=0.7):
    '''
    This function calculates the eligibility trace of strategies based on game.nasheq.
    :param nasheq_dict: {"baselines": game.nasheq}
    :param gamma:
    :return:
    '''
    position = find_heuristic_position(child_partition)
    total_num_str = 0
    for method in child_partition:
        total_num_str += child_partition[method]

    et_dict_def = {}
    et_dict_att = {}

    nash_thred = 0.05

    # Construct eligibility trace.
    for method in nasheq_dict:
        et_dict_def[method] = np.zeros(child_partition[method])
        et_dict_att[method] = np.zeros(child_partition[method])
        nasheq = nasheq_dict[method]
        for epoch in np.arange(1, child_partition[method] + 1):
            et_dict_att[method] *= gamma
            ne_att = nasheq[epoch][1][1:]
            ne_att[ne_att <= nash_thred] = 0
            ne_att[ne_att > nash_thred] = -2
            et_dict_att[method][:len(ne_att)] += ne_att

            et_dict_def[method] *= gamma
            ne_def = nasheq[epoch][0][1:]
            ne_def[ne_def <= nash_thred] = 0
            ne_def[ne_def > nash_thred] = -2
            et_dict_def[method][:len(ne_def)] += ne_def

    # Put strategies into the queue with the eligibility trace as priority.
    pq_def = {}
    pq_att = {}
    for method in et_dict_def:
        pq_def[method] = pq()
        pq_att[method] = pq()
        start, end = position[method]
        idx_str = start + np.arange(child_partition[method])
        idx_et_pair_def = zip(et_dict_def[method], idx_str)
        idx_et_pair_att = zip(et_dict_att[method], idx_str)
        for pair in idx_et_pair_def:
            pq_def[method].put(pair)

        for pair in idx_et_pair_att:
            pq_att[method].put(pair)

    return pq_def, pq_att
Esempio n. 2
0
def prim(origen):
    global aeropuertos, visited
    mst = coste_aeropuerto
    aeropuertos += 1
    #pq = [(coste, destino) for coste, destino in graph[origen]]
    q = pq()
    for coste, destino in graph[origen]:
        q.put((coste, destino))
    #visited.add(origen)
    visited[origen] = True
    #heapq.heapify(pq)

    while not q.empty():
        #coste, destino = heapq.heappop(pq)
        coste, destino = q.get()
        if destino not in visited:
            #visited.add(destino)
            visited[destino] = True
            if coste >= coste_aeropuerto:
                mst += coste_aeropuerto
                aeropuertos += 1
            else:
                mst += coste
            for cost, next_ in graph[destino]:
                if next_ not in visited:
                    #heapq.heappush(pq, (cost, next_))
                    q.put((cost, next_))
                else:
                    continue
        else:
            continue

    return mst
Esempio n. 3
0
    def prims(self):
        mst = defaultdict(list)
        cost = 0
        visited = set()
        q = pq()

        q.put((0, (0, 0)))

        while not q.empty() and len(mst) < len(self.vertexLbl):
            weight, (src, dest) = q.get()

            if src != dest and dest not in visited:
                mst[self.vertices[src]].append((self.vertices[dest], weight))
                mst[self.vertices[dest]].append((self.vertices[src], weight))
                cost += weight
                print(f'{self.vertices[src]} -> {self.vertices[dest]} ({weight} ({cost}))')

            visited.add(dest)

            for adjVertex in range(len(self.adjMat[dest])):

                if adjVertex not in visited and dest != adjVertex and self.adjMat[dest][adjVertex] >= 0:
                    q.put((self.adjMat[dest][adjVertex], (dest, adjVertex)))

            

        pp(mst)

        return cost
    def KruskalMST(self, node):
        weights = 0  # The weight of our MST
        nodes = self.vertices  # The vertices of our graph
        nodes.append(node)  # Add the node given
        cost = pq()  # A priority queue for holding the cost of each node.
        rank = {}  # Dictionary of ranks for each node.
        parent = {}  # Dictionary of parents for each node.
        for u in nodes:  # For each node u
            parent[u] = u  # Make it its own parent for now
            rank[u] = math.inf  # Make the rank infinity for now
            for v in nodes:  # For each node v
                if u != v:
                    cost.put(
                        (heuristic(u, v), u, v)
                    )  # If u is not the same node as v, we will calculate the cost and put it in the priority queue

        while len(self.edges) < len(
                self.vertices) - 1:  # Go through EVERY edge. |E| = |V|-1
            smallest = cost.get()  # Get the minimum cost between two nodes u,v
            x = self.find(parent, smallest[1])  # Set x as the root of node u
            y = self.find(parent, smallest[2])  # Set y as the root of node v
            if x != y:  # If x and y are not the same node
                self.addEdge(
                    smallest[1], smallest[2],
                    heuristic(smallest[1],
                              smallest[2]))  # Add an edge between them
                self.union(rank, parent, x, y)  # Union them.
        for edge in self.edges:
            weights += edge[2]  # Sum the edge weights.
        return weights
Esempio n. 5
0
def c_path(G, v):
    D = numpy.copy(G[v])
    P = [-1 for i in range(D.shape[0])]
    final = [0 for i in range(D.shape[0])]
    final[v] = 1
    Qu = pq()
    for i in range(D.shape[0]):
        Qu.put((D[i], i))
    D[v]=0
    for i in range(D.shape[0]):
        if sum(final) == D.shape[0]:
            break
        if i != v:
            ite = Qu.get()
            key = ite[1]
            while final[key] != 0:
                ite = Qu.get()
                key = ite[1]
            k = key
            mini = D[key]
            final[k] = 1
            for j in range(D.shape[0]):
                if final[j] == 0 and (mini + G[k, j] < D[j]):
                    D[j] = mini + G[k, j]
                    Qu.put((D[j], j))
                    P[j] = k
    pattern = {-1:v}
    P = [x if x not in pattern else pattern[x] for x in P]
    return D, P, final
Esempio n. 6
0
def Astarh2(start_node):
    PQueue = pq()
    visited = []
    explored = 0
    PQueue.put((start_node.calcmanhattan_distance(), start_node))
    while not PQueue.empty():
        h, n = PQueue.get()
        if n.b in visited:
            continue
        if h == 0:
            print('Testing h2...')
            print('Congratulations!')
            print('The number of explored nodes:%d' % explored)
            print('The number of steps taken:', (len(n.node_traceback())) - 1)
            print()
            #draw_path(n.node_traceback())
            #draw_board(start_node.b)
            return

        visited.append(n.b)

        explored += 1

        for nnode in n.create_nodes():
            PQueue.put((nnode.calcmanhattan_distance(), nnode))
Esempio n. 7
0
    def trapRainWater(self, heightMap: [[int]]) -> int:
        row = len(heightMap)
        col = len(heightMap[0])

        been = [[0]*col for _ in range(row)]

        que = pq()
        ans = 0

        for i in range(col):
            been[0][i] = been[row-1][i] = 1
            que.put( (heightMap[0][i], 0, i) )
            que.put( (heightMap[row-1][i], row-1, i) )
        for j in range(1, row-1):
            been[j][0] = been[j][col-1] = 1
            que.put( (heightMap[j][0], j, 0) )
            que.put( (heightMap[j][col-1], j, col-1) )

        dr = [0,0,1,-1]
        dc = [1,-1,0,0]
        while not que.empty():
            h, r, c = que.get()
            for i in range(4):
                nr = r + dr[i]
                nc = c + dc[i]
                if nr>=0 and nr<row and nc>=0 and nc<col and not been[nr][nc]:
                    # print("cao?", nr, nc, h - heightMap[nr][nc])
                    ans += max(h-heightMap[nr][nc], 0)
                    been[nr][nc] = 1
                    que.put((max(h, heightMap[nr][nc]), nr, nc))
        return ans
Esempio n. 8
0
def lazyprims(source, numberofnodes):

    mstcost = 0
    p1 = pq()
    mstedge = []
    visited = set()
    visited.add(source)
    for edge in graph[source]:
        node, weight = edge
        p1.put((weight, source, node))
    while not p1.empty():

        k = p1.get()

        w, s, d = k
        mstedge.append((s, d))

        s = d
        mstcost += w

        for edge in graph[s]:

            node, weight = edge
            if node in visited:
                continue
            p1.put((weight, d, node))

        visited.add(s)
    print("mst cost is {0}".format(mstcost))

    return mstedge
Esempio n. 9
0
def initial(Adj, s):
    """G: the graph representation; s: the initial node; return a priority queue which store nodes and the key value of each nodes is the node.d attributes."""
    Q = pq()
    for i in Adj.keys():
        u = node(i, s)
        if u.name == s:
            u.d = 0
        Q.put((u.d, u))  #use Q to preserve the collection of nodes.
    return Q
Esempio n. 10
0
 def get_distances(self):
     weights = Distances(self)
     pending = pq()
     pending.put(self)
     while pending.qsize() >= 1:
         cell = pending.get()
         for neighbor in cell.links:
             total_wt = weights[cell] + neighbor.weight
             if not weights[neighbor] or total_wt < weights[neighbor]:
                 pending.put(neighbor)
                 weights[neighbor] = total_wt
     return weights
Esempio n. 11
0
 def mergeKLists(self, lists) -> ListNode:
     Q = pq()
     for l in lists:
         while l != None:
             Q.put(l.val)
             l = l.next
     ans = ListNode()
     first = ans
     while not Q.empty():
         l = Q.get()
         first.next = ListNode(l)
         first = first.next
     return ans.next
Esempio n. 12
0
 def solve(self):
     initNode = self.newnode(puzzle=self._puzzle)
     listopen = pq()
     listclose = []
     listopen.put(initNode)
     while listopen:
         n = listopen.get()
         listclose.append(n)
         if n.isfinished:
             self.totalnodecount = listopen.qsize() + len(listclose)
             return n
         for ext in self.extendnode(n):
             listopen.put(ext)
     return False
Esempio n. 13
0
    def bfs():
        nonlocal answer
        q = pq()
        seen[0][0] = 1
        q.put((0, 0, 0, 1))  #length, x, y, direction
        while q.queue:
            l, x, y, d = q.get()

            if (x == N and y == N) or (x + dx[d] == N and y + dy[d] == N):
                answer = min(answer, l)

            for i in range(1, 5):
                nx, ny = x + dx[i], y + dy[i]
                nxx, nyy = nx + dx[d], ny + dy[d]

                if nx < 0 or nx > N or ny < 0 or ny > N:
                    continue
                if nxx < 0 or nxx > N or nyy < 0 or nyy > N:
                    continue
                if board[nx][ny] == 1 or board[nxx][nyy] == 1:
                    continue

                if (seen[nx][ny] & dd[d]) != dd[d]:
                    q.put((l + 1, nx, ny, d))
                    seen[nx][ny] += dd[d]

            for i in [2, 4]:
                nd = 1 + (d + i) % 4
                if x + dx[nd] < 0 or x + dx[nd] > N or y + dy[
                        nd] < 0 or y + dy[nd] > N:
                    continue
                if x + dx[nd] + dx[d] < 0 or x + dx[nd] + dx[d] > N or y + dy[
                        nd] + dy[d] < 0 or y + dy[nd] + dy[d] > N:
                    continue
                if board[x + dx[nd]][y +
                                     dy[nd]] == 1 or board[x + dx[nd] +
                                                           dx[d]][y + dy[nd] +
                                                                  dy[d]] == 1:
                    continue

                if (seen[x][y] & dd[nd]) != dd[nd]:
                    q.put((l + 1, x, y, nd))
                    seen[x][y] += dd[nd]
                if (seen[x + dx[d]][y + dy[d]] & nd) != nd:
                    q.put((l + 1, x + dx[d], y + dy[d], nd))
                    seen[x + dx[d]][y + dy[d]] += dd[nd]

        return
Esempio n. 14
0
 def topological_sort(self, graph):
     q = pq()
     topo_sorted = []
     for i in range(graph.vertex_cnt):
         if graph.vertexs[i].in_degree == 0:
             q.put(graph.vertexs[i])
     while not q.empty():
         node = q.get()
         print(node.idx)
         topo_sorted.append(node.idx)
         for i in range(node.adjacent):
             adjacent_idx = node.adjacent_nodes[i]
             graph.vertexs[adjacent_idx].in_degree -= 1
             if graph.vertexs[adjacent_idx].in_degree == 0:
                 q.put(graph.vertexs[adjacent_idx])
     return topo_sorted if len(topo_sorted) == graph.vertex_cnt else None
    def solve(self, initial_node):
        PQ = pq()
        visited = []
        explored = 0
        PQ.put((initial_node.manhattan_distance(), initial_node))
        while not PQ.empty():
            h, n = PQ.get()
            if n.board in visited:
                continue
            if h == 0:
                self.draw(n.track())
                return

            visited.append(n.board)
            explored += 1
            for nnode in n.generate_nodes():
                PQ.put((nnode.manhattan_distance(), nnode))
Esempio n. 16
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     stack = pq()
     for node in lists:
         if node:
             stack.put((node.val, node))
     dummy = ListNode(None)
     cur = dummy
     while stack.qsize() > 0:
         val, node = stack.get()
         cur.next = node
         cur = cur.next
         if node.next:
             stack.put((node.next.val, node.next))
     return dummy.next
    def kthSmallest(self, matrix: List[List[int]], k: int) -> int:
        n = len(matrix)
        from queue import PriorityQueue as pq
        q = pq()

        for i in range(n):
            q.put((matrix[i][0], i, 0))

        count = 0

        while count < k:
            item, row, idx = q.get()
            count += 1
            if idx >= n - 1:
                continue
            q.put((matrix[row][idx + 1], row, idx))

        return item
Esempio n. 18
0
def solution(board):
    SIZE = len(board)
    OPEN, WALL = range(2)
    START1 = (0, 0, 0, 0)  #cost, x, y, d
    START2 = (0, 0, 0, 1)
    END_POINT = (SIZE - 1, SIZE - 1)
    DELTAS = ((0, 1), (1, 0), (0, -1), (-1, 0))  #0, 1, 2, 3

    q = pq()
    q.put(START1)
    q.put(START2)
    visited = set()
    visited.add(START1)
    visited.add(START2)

    def _is_in_range(x, y):
        return 0 <= x < SIZE and 0 <= y < SIZE

    def _is_open(x, y):
        return board[x][y] == OPEN

    def _is_ok(x, y):
        return _is_in_range(x, y) and _is_open(x, y)

    def _yield_moves(cost, x, y, d):
        for i in [d, (d + 1) % 4, (d + 3) % 4]:
            dx, dy = DELTAS[i]
            nx, ny = x + dx, y + dy
            if _is_ok(nx, ny) and _is_ok(nx, ny):
                if i == d:
                    yield (cost + 100, nx, ny, i)
                else:
                    yield (cost + 600, nx, ny, i)

    while q.queue:
        cost, x, y, d = q.get()
        if (x, y) == END_POINT:
            return cost
        for next_road in _yield_moves(cost, x, y, d):
            if next_road not in visited:
                q.put(next_road)
                visited.add(next_road)

    return
Esempio n. 19
0
 def shortest_path(self, id1: int, id2: int) -> (float, list):
     """
     Returns the shortest path from node id1 to node id2 using Dijkstra's Algorithm
     @:param id1 - The start node id
     @:param id2 - The end node id
     @:return The distance of the path, a list of the nodes ids that the path goes through
     """
     if self._graph is None:
         return float('inf'), []
     all_nodes = self._graph.get_all_v()
     if all_nodes.get(id1) is None or all_nodes.get(id1) is None:
         return float('inf'), []
     if id1 == id2:
         return 0, [id1]
     for k, v in all_nodes.items():
         v.set_weight(float('inf'))
     first_node = all_nodes[id1]
     first_node.set_weight(0)
     queue = pq()
     queue.put((first_node.get_weight(), first_node))
     while not queue.empty():
         first_node = queue.get()[1]
         for dest, weight in self._graph.all_out_edges_of_node(
                 first_node.get_key()).items():
             if all_nodes[dest].get_weight(
             ) > first_node.get_weight() + weight:
                 all_nodes[dest].set_weight(first_node.get_weight() +
                                            weight)
                 queue.put((all_nodes[dest].get_weight(), all_nodes[dest]))
     dest_node = all_nodes[id2]
     dist = dest_node.get_weight()
     if dist == float('inf'):
         return float('inf'), []
     path = [dest_node.get_key()]
     while dest_node.get_weight() != 0:
         edges_into_node = self._graph.all_in_edges_of_node(
             dest_node.get_key())
         for k, v in edges_into_node.items():
             if dest_node.get_weight() == all_nodes[k].get_weight() + v:
                 dest_node = all_nodes[k]
                 path.insert(0, dest_node.get_key())
                 break
     return dist, path
Esempio n. 20
0
    def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]:
        from queue import PriorityQueue as pq
        map_ = {i: j for j, i in enumerate(arr2)}

        p = pq()
        appear = []
        for i in arr1:
            if i in map_.keys():
                p.put((map_[i], i))
            else:
                appear.append(i)

        ans = []
        while not p.empty():
            ans.append(p.get())

        ans.entend(sorted(appear))

        return ans
Esempio n. 21
0
def dijkstra():
    heap = pq()
    heap.put((1, (0, 0)))
    crush[0][0] = 0
    # 상하좌우
    dx, dy = [-1, 1, 0, 0], [0, 0, -1, 1]
    while True:
        cnt, (hi, hj) = heap.get()
        # print(cnt, hi, hj)
        if hi == n - 1 and hj == m - 1:
            break
        for d in range(4):
            x, y = hi + dx[d], hj + dy[d]
            if 0 <= x < n and 0 <= y < m and crush[x][y] < 0:
                # crush[x][y] = (x,y)전까지 부순 벽의 갯수
                crush[x][y] = cnt
                if maze[x][y] == '1':
                    # heap의 원소의 cnt = (x,y)에서 부순 벽의 갯수도 포함
                    heap.put((cnt + 1, (x, y)))
Esempio n. 22
0
def shortest_path(M, start, goal):
    frontier = pq()    # priority queue
    frontier.put(start, 0)
    g = {start: 0}    # distance from start to a node.
    previous = {start: None}
    while not frontier.empty():
        current = frontier.get()    # gets the least priority value, in this case it gets the one with least distance.

        if current == goal:
            continue
        
        for neighbor in M.roads[current]:
            temp = g[current] + h(M.intersections[current], M.intersections[neighbor])
            if neighbor not in g or temp < g[neighbor]:    # temp < g[neighbor] - to select the shortest path and explore min no. of nodes.
                previous[neighbor] = current    # remembering where the neighbor came from.
                g[neighbor] = temp  
                f = temp + h(M.intersections[neighbor], M.intersections[goal])    # f = g + h
                frontier.put(neighbor, f)    # adding neighbor with its total distance as priority.
           
    return path(previous, start, goal)
Esempio n. 23
0
def prim(G, r):
    global V
    U = set()
    U.add(r)
    Q = pq()
    summ = 0

    for idx, num in enumerate(G[r]):
        if num:
            Q.put((num, idx))

    while not Q.empty():
        t = Q.get()
        if t[1] not in U:
            U.add(t[1])
            summ += t[0]
            for idx, num in enumerate(G[t[1]]):
                if num and idx not in U:
                    Q.put((num, idx))

    return summ
def solve(start_node, goal):
    PQueue = pq()
    visited = []
    explored = 0
    PQueue.put((start_node.dist(goal), start_node))
    while not PQueue.empty():
        h, n = PQueue.get()
        #h=h+n.height # A star
        if n.mat in visited:
            continue
        if h == 0:
            print_path(n.path())
            print('explored nodes:%d' % explored)
            return

        visited.append(n.mat)

        explored += 1

        for nnode in n.FindChildren(n.height + 1):
            PQueue.put((nnode.dist(goal), nnode))
Esempio n. 25
0
def Astar(initial_node):
    PQueue = pq()  #priority queue, compares and sorts.
    visited = []
    explored = 0
    PQueue.put((initial_node.manhattan_distance(),
                initial_node))  #inserts a tuple with heuristic and node.
    while not PQueue.empty():
        h, n = PQueue.get()  #PQueue returns a tuple, heuristic and node.
        if n.board in visited:  #this prevents repetition of the nodes.
            continue
        if h == 0:  #this is a condition for winning.
            print('Congratulations!')
            print('The numbered of explored nodes:%d' % explored)
            draw_path(n.traceback())
            return

        visited.append(n.board)

        explored += 1

        for nnode in n.generate_nodes():
            PQueue.put((nnode.manhattan_distance(), nnode))
def astar(maze):
    """
    Runs A star for part 1 of the assignment.

    @param maze: The maze to execute the search on.

    @return path: a list of tuples containing the coordinates of each state in the computed path
    """
    # TODO: Write your code here
    objectives = maze.getObjectives(
    )  # Just get the objectives first to check edge case (start == goal)
    frontier = []  # Empty queue since it is A*
    start = maze.getStart()  # Starting point
    frontier.append(start)  # Add the start node to frontier
    if (start == objectives[0]):  # If we start at the objective, return.
        return frontier
    explored = []  # explored states
    cost = pq()  # Priority queue of nodes. Holds them by distance.
    cost.put((heuristic(start, objectives[0]), frontier))
    path = []
    while (cost.empty() !=
           True):  # While the priority queue is not empty we loop
        path = cost.get()[1]  # Get the current frontier/path
        node = path[len(path) - 1]  # Get the most recent one.
        if node in explored:
            continue
        if node == objectives[0]:
            return path
        explored.append(node)  # Explored the node
        neighbors = maze.getNeighbors(node[0], node[1])  # Get the neighbors
        for neighbor in neighbors:  # For each neighbor
            manhattanDist = heuristic(
                neighbor, objectives[0])  # Calculate the manhattan distance
            if (neighbor not in explored) or (neighbor not in frontier) and (
                    maze.isValidMove(neighbor[0], neighbor[1])):
                frontier = list(path)  # Set the path as the frontier.
                frontier.append(neighbor)  # Add the neighbor to the frontier.
                cost.put((manhattanDist + len(frontier),
                          frontier))  # Push the new cost.
Esempio n. 27
0
def Astar(init_state, goal_state):
    PQueue = pq()
    visited = []
    explored = 0
    PQueue.put((init_state.l1_distance(goal_state), init_state))

    while not PQueue.empty():
        h, n = PQueue.get()  #heuristic and node

        if n.board in visited:
            continue

        if h == 0:  # Gameset
            print('Gameset!')
            print(f'Explored states {explored}')
            draw_path(n.traceback())
            return

        visited.append(n.board)
        explored += 1

        for nstate in n.generate_states():
            PQueue.put((nstate.l1_distance(goal_state), nstate))
Esempio n. 28
0
 def shortest_paths(graph, source_node):
     distances = dict()
     edges = pq()
     distances[source_node] = 0
     for edge in graph.alist[source_node]:
         if edge.node1 == source_node:
             edges.put(edge.clone())
         else:
             edges.put(Edge(edge.node2, edge.node1, edge.weight))
     while len(distances) < len(graph.alist) and edges:
         next_edge = edges.get()
         if next_edge.node2 not in distances:
             next_node = next_edge.node2
             distances[next_node] = next_edge.weight
             for edge in graph.alist[next_node]:
                 next_next_node = edge.node2 if edge.node1 == next_node else edge.node1
                 if next_next_node not in distances:
                     edges.put(Edge(next_node, next_next_node, next_edge.weight + edge.weight))
                 # if edge.node1 == next_node:
                     # edges.put(Edge(next_node, edge.node2, next_edge.weight + edge.weight))
                 # else:
                     # edges.put(Edge(next_node, edge.node1, next_edge.weight + edge.weight))
     return distances
Esempio n. 29
0
def dijkstra(graph, s):
    """graph: graph presentated by adjacent list. s: start point"""
    q = pq()  #用来保存计算了到s路径长度的节点。
    q.put((0, s))  #最短路径从初始点s开始
    seen = set()  #最短路径节点集合
    seen.add(s)  #加入初始点
    parents = {s: None}  #保存最短路径上节点的 predecessor
    distance = {}  # 保存最短路径上节点到s的距离
    for node in graph:
        distance[node] = inf
    distance[s] = 0

    while len(seen) < len(graph):
        dist_u, u = q.get()
        seen.add(u)
        neighbors = graph[u]
        for node in neighbors:
            if node not in seen:
                dist_node = dist_u + graph[u][node]
                if dist_node < distance[node]:
                    q.put((dist_node, node))
                    distance[node] = dist_node
                    parents[node] = u
    return parents, distance
Esempio n. 30
0
from queue import PriorityQueue as pq

dr = [1, 0, -1, 0]
dc = [0, 1, 0, -1]

for tc in range(1, int(input()) + 1):
    N = int(input())
    map = [[int(n) for n in input()] for _ in range(N)]
    djikstra = [[90000] * N for _ in range(N)]
    que = pq()
    que.put((0, 0, 0))
    djikstra[0][0] = 0
    while que:
        d, r, c = que.get()
        if d > djikstra[N - 1][N - 1]:
            break
        for i in range(4):
            nr, nc = r + dr[i], c + dc[i]
            if 0 <= nr < N and 0 <= nc < N:
                nd = d + map[nr][nc]
                if djikstra[nr][nc] > nd:
                    djikstra[nr][nc] = nd
                    que.put((nd, nr, nc))
    print(f'#{tc} {djikstra[N-1][N-1]}')
Esempio n. 31
0
from queue import PriorityQueue as pq

T = int(input())
for t in range(1, T + 1):
    n, k = map(int, input().split())
    p = pq()
    p.put(-n)
    for i in range(k - 1):
        r = -p.get()
        p.put(-(r // 2))
        p.put(-((r - 1) // 2))
    r = -p.get()
    hi = r // 2
    lo = (r - 1) // 2
    print("Case #%d: %d %d" % (t, hi, lo))
Esempio n. 32
0
# Author :  Abhyuday Tripathi
import sys
from termcolor import colored, cprint
from copy import deepcopy
from queue import PriorityQueue as pq
frontier = pq()
explored = dict()
import time


class board:
    def __init__(self, state):
        self.state = state
        self.parent = None
        self.level = 0
        self.ch = []
        self.loners = 0
        self.actions = 0
        self.gots = 0
        self.hvalue = 0

    def isolation(self):
        check = False

        for i in range(7):
            for j in range(7):
                h1 = False
                h2 = False
                v1 = False
                v2 = False
                if (self.state[i][j] == 1):