Example #1
0
def z(n, b):
    # change base from decimal int "n" to base b
    p = g.digits + g.ascii_lowercase
    r = q()  # queue
    while n > 0:
        w = n % b  # least significant digit
        r.appendleft(p[w])
        n //= b
    return ''.join(r)
Example #2
0
def bfs(top, now, target, up, down):
    dy = [up, -down]
    queue = q([(now, 0)])
    visit = {now}
    while queue:
        y, cnt = queue.popleft()
        if y == target:
            return cnt
        if y <= top and y > 0:
            for i in dy:
                ny = y + i
                if ny not in visit and ny <= top and ny >= 1:
                    visit.add(ny)
                    queue.append((ny, cnt + 1))
    return "use the stairs"
Example #3
0
def number_of_paths(G, start, stop):
    edges = q()
    g = []
    path_num = 0
    for i in G:
        if i[0] == start:
            edges.append(i)
        else:
            g.append(i)
    G = g
    while len(edges) > 0:
        iter = edges.popleft()
        if iter[1] == stop:
            path_num += 1
        path_num += number_of_paths(G, iter[1], stop)
    return path_num
Example #4
0
def bfs():
    queue = q([(0, 0, 1)])
    visit = [[[0] * 2 for i in range(m)] for i in range(n)]
    visit[0][0][1] = 1
    dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
    while queue:
        x, y, Count = queue.popleft()
        if x == n - 1 and y == m - 1:
            return visit[x][y][Count]
        for i in range(4):
            nx, ny = x + dx[i], y + dy[i]
            if 0 <= nx < n and 0 <= ny < m:
                if matrix[nx][ny] == 0 and visit[nx][ny][Count] == 0:
                    queue.append((nx, ny, Count))
                    visit[nx][ny][Count] = visit[x][y][Count] + 1
                if matrix[nx][ny] == 1 and Count == 1:
                    queue.append((nx, ny, 0))
                    visit[nx][ny][0] = visit[x][y][1] + 1
    return -1
 def __init__(self, size: int):
     self._q = q()
     self._size = size
Example #6
0
'''
미로탈출
사용한 자료구조: BFS, 큐
시간 복잡도: O(N^2)
'''

from collections import deque as q
#입력 받기
n, m = map(int, input().split())
matrix = []
for i in range(n):
    matrix.append([int(i) for i in input()])
#큐(초기값 0,0),방문확인배열(초기값 0,0),방향벡터
queue = q()
queue.append((0, 0))
visit = [(0, 0)]
dx, dy = [1, -1, 0, 0], [0, 0, 1, -1]
#방문 안한 모든 좌표에대해 확인
while (queue):
    #맨 앞의 좌표 pop
    x, y = queue.popleft()
    # 네방향 확인 // (1,0),(-1,0),(0,1),(0,-1)
    for i in range(4):
        nx, ny = x + dx[i], y + dy[i]
        #범위 밖이거나 벽일 경우
        if nx < 0 or ny < 0 or nx >= n or ny >= m or matrix[nx][ny] == 0:
            continue
        #방문하지 않은경우
        if (nx, ny) not in visit:
            visit.append((x, y))
            queue.append((nx, ny))
from collections import deque as q

n = 5  # in mins
unit = 1  # seconds
q_size = (5 * 60) / 1
curr_count = 0
web_hit_q = q([])


# continuously tracking the count.
def collect_hits():
    curr_count += 1


# called after every unit time
def record_hits():
    if len(web_hit_q) > q_size:
        web_hit_q.popleft()
    web_hit_q.append(curr_count)
    curr_count = 0


def get_hits():
    no_of_users = 0
    for i in range(len(web_hit_q)):
        no_of_users += web_hit_q[i]

    return no_of_users
Example #8
0
'''
from collections import deque as q
import sys

n, k = map(int, sys.stdin.readline().split())
matrix = []
temp = []
# 입력받고 정렬후 큐에 넣기
for i in range(n):
    matrix.append([i for i in list(map(int, sys.stdin.readline().split()))])
    for j in range(n):
        if matrix[i][j] != 0:
            temp.append([matrix[i][j], i, j, 0])
s, x, y = map(int, input().split())
temp.sort(key=lambda x: x[0])
q = q(temp)
dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]

#전염 시작
while q:
    val, nx, ny, ns = q.popleft()
    #찾는 위치에 값이 들어온 경우 혹은 시간이 다된 경우
    if nx == x - 1 and ny == y - 1 or ns == s:
        break
    for i in range(4):
        ix = nx + dx[i]
        iy = ny + dy[i]
        # 범위 안에 있고 감엽시킬수있으면 큐에 저장
        if ix >= 0 and ix < n and iy >= 0 and iy < n:
            if matrix[ix][iy] == 0: