Algorithm_BFS_Python

It's me, Hyeseung·2022년 9월 24일
0

Algorithm

목록 보기
1/8
post-thumbnail

BFS

from collections import deque
q = deque()
q.append(2)
q.append(4)
q.append(9)
print(*q)
q.appendleft(5)
print(*q)
x = q.popleft()
print(*q)
# A B C D E F BFS로 구현하기
arr = list(input().split())
arr2 = [[0,1,0,1,0,0],
        [0,0,1,0,1,0],
        [0,0,0,0,0,0],
        [0,0,0,0,0,1],
        [0,0,0,0,0,0],
        [0,0,0,0,0,0],

]
def bfs(st):
    global answer
    q = deque()
    q.append(st)
    while q:
        now = q.poplefft()
        answer.append(name[now])
        for i in range(6):
            if arr[now][i] == 1:
                    q.append(i)
                    used[i]=1
bfs(0)
print(*answer)
#중복 없이 BFS 구현하기
def bfs(st):
    global answer
    q = deque()
    q.append(st)
    while q:
        now = q.popleft()
        answer.append(name[now])
        for i in range(4):
            if arr[now][i] == 1:
                if used[i] == 0:
                    q.append(i)
                    used[i] = 1
  
name = list(input().split())
arr = [[0,1,1,0],
       [0,0,1,1],
       [0,1,0,1],
       [0,0,0,0],
        ]
answer = []
used = [0]*4
used[1] = 1
bfs(1)
print(*answer)
#개화시기 문제다 이놈들아
n = int(input())
y,x = map(int,input().split())
arr = [[0]*n for _ in range(n)]

def bloom(y,x):
    global arr,n
    arr[y][x] = 1
    q = deque()
    q.append([y,x])
    while q:
        now = q.popleft()
        y,x = now[0],now[1]
        dy = [-1,1,0,0]
        dx = [0,0,1,-1]
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny <=n and 0<= nx <=n:
                if arr[ny][nx] == 0:
                    arr[ny][nx] = arr[y][x] +1
                    q.append([ny,nx])
bloom(y,x)
for i in arr:
    print(*i)
#씨앗 두개받는 문제다 이놈들아,,
def bfs(lst):
    q = deque()
    q.append(lst[0])
    q.append(lst[1])
    while q:
        now = q.popleft()
        y = now[0]
        x = now[1]
        dy = [-1,1,0,0]
        dx = [0,0,-1,1]
        for i in range(4):
            ny = y + dy[i]
            nx = x + dx[i]
            if 0 <= ny < n and 0 <=nx <n:
                if arr[ny][nx] == 0:
                    day = arr[y][x]
                    arr[ny][nx] = arr[y][x] +1
                    q.append([ny, nx])
    return day
    
n = int(input())
arr = [[0]*n for _ in range(n)]
lst = [list(map(int,input().split())) for _ in range(2)]
for i in range(2):
    y,x = lst[i]
    arr[y][x] = 1
print(bfs(lst))
for i in arr:
    print(*i)

0개의 댓글