๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • Welcome.
:: Python ๐Ÿšฉ/์•Œ๊ณ ๋ฆฌ์ฆ˜

BFS & DFS

by EunBird 2023. 2. 16.
stack = []

stack.append(5) #push 5
stack.append(2) #push 2
stack.append(3) #push 3
stack.append(7) #push 7
stack.pop()
stack.append(1) #push 1
stack.append(4) #push 4
stack.pop()

print(stack[::-1]) #์ตœ์ƒ๋‹จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ
print(stack) #์ตœํ•˜๋‹จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ

 

from collections import deque

queue = deque()

queue.append(5)
queue.append(2)
queue.append(3)
queue.append(7)
queue.popleft()
queue.append(1)
queue.append(4)
queue.popleft()

print(queue) # ๋จผ์ € ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅ.
queue.reverse() # ์—ญ์ˆœ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ
print(queue) # ๋‚˜์ค‘์— ๋“ค์–ด์˜จ ์›์†Œ๋ถ€ํ„ฐ ์ถœ๋ ฅ.

 

def factorial_iterative(n): # ๋ฐ˜๋ณต์ ์œผ๋กœ ๊ตฌํ˜„
    result = 1
    for i in range(1, n+1):
        result = result * i
    return result

def factorial_recursive(n): # ์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„
    if n<=1:
        return 1
    else :
        return n*factorial_recursive(n-1)

print('๋ฐ˜๋ณต์ ์œผ๋กœ ๊ตฌํ˜„:', factorial_iterative(5))
print('์žฌ๊ท€์ ์œผ๋กœ ๊ตฌํ˜„:', factorial_recursive(5))

 

def gcd(a,b):
    if a % b == 0:
        return b
    else :
        return gcd(b,a % b)

print(gcd(192,162))
def dfs(graph, v ,visited):
    visited[v] = True #๋ฐฉ๋ฌธ ์ฒ˜๋ฆฌ
    print(v, end=' ')
    for i in graph[v]:
        if visited[i] == False: #์•„์ง ๋ฏธ๋ฐฉ๋ฌธ์ด๋ผ๋ฉด
            dfs(graph, i, visited)

graph = [
    [],
    [2,3,8],
    [1,7],
    [1,4,5],
    [3,5],
    [3,4],
    [7],
    [2,6,8],
    [1,7]
]

visited = [False] * 9
dfs(graph, 1, visited) # ์‹œ์ž‘ ๋…ธ๋“œ=1

 

from collections import deque

def bfs(graph, start, visited):
    queue = deque([start])
    visited[start] = True # ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

    while queue: #ํ๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€
        v = queue.popleft() # ์ž‘์€ ์ˆœ์„œ๋Œ€๋กœ
        print(v, end =' ')

        for i in graph[v]: # ์ธ์ ‘๋…ธ๋“œ๋“ค์— ๋Œ€ํ•ด
            if not visited[i]:
                queue.append(i)
                visited[i] = True # ๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

graph = [
    [],
    [2,3,8],
    [1,7],
    [1,4,5],
    [3,5],
    [3,4],
    [7],
    [2,6,8],
    [1,7]
]

visited = [False] * 9
bfs(graph, 1, visited) # ์‹œ์ž‘ ๋…ธ๋“œ=1

 

def dfs(x,y):

    if x < -1 or x >= N or y <= -1 or y >= M:
        return False

    if graph[x][y] == 0: #๋ฏธ๋ฐฉ๋ฌธ์‹œ
        graph[x][y] = 1 #๋ฐฉ๋ฌธ์ฒ˜๋ฆฌ

        dfs(x-1,y)
        dfs(x,y-1)
        dfs(x+1,y)
        dfs(x,y+1)
        return True
    else:
        return False #[x][y]๊ฐ€ ๋ฐฉ๋ฌธ ๋…ธ๋“œ์ธ ๊ฒฝ์šฐ


N,M = map(int,input().split())

graph = []
for i in range(N):
    graph.append(list(map(int,input())))

result = 0
for i in range(N):
    for j in range(M):
        if dfs(i,j) == True:
            result += 1
print(result)

 

 

from collections import deque
def bfs(x,y):
    queue = deque()
    queue.append((x,y))
    
    while queue:
        x,y = queue.popleft()
        
        for i in range(4):
            nx = x + dx[i]
            ny = y + dy[i]
            if nx < 0 or nx >= n or ny < 0 or ny >= m:
                continue
            if graph[nx][ny] == 0: #๋ฐฉ๋ฌธ์œ„์น˜์ด๋ฉด ์•ˆ๊ฐ
                continue
            if graph[nx][ny] == 1: #๋ฏธ๋ฐฉ๋ฌธ์œ„์น˜์ด๋ฉด ๊ฐ
                graph[nx][ny] = graph[x][y] + 1
                queue.append((nx,ny))
    return graph[n-1][m-1]


n,m = map(int,input().split())

graph = []
for i in range(n):
    graph.append(list(map(int,input())))

# ์ด๋™ ๋ฐฉํ–ฅ [์ƒ,ํ•˜,์ขŒ,์šฐ]
dx = [-1,1,0,0]
dy = [0,0,-1,1]

print(bfs(0,0))
728x90

๋Œ“๊ธ€