함께하는 데이터 분석

[이것이 코딩테스트다] 이코테 파이썬 DFS/BFS 본문

코딩 테스트/이것이 코딩테스트다

[이것이 코딩테스트다] 이코테 파이썬 DFS/BFS

JEONGHEON 2023. 8. 7. 15:04

음료수 얼려 먹기

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

def dfs(x, y) :
    if x < 0 or x >= n or y < 0 or y >= m :
        return False
    
    if graph[x][y] == 0 :
        graph[x][y] = 1
        dfs(x - 1, y)
        dfs(x + 1, y)
        dfs(x, y - 1)
        dfs(x, y + 1)
        return True
    return False

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

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]

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]

print(bfs(0, 0))