[Algorithm] 백준 1261 알고스팟 | 파이썬 BFS
※ 1261 알고스팟https://www.acmicpc.net/problem/1261 문제 해결 TIP이전 미로만들기와 동일한 문제이다. n, m 값의 범위만 재설정해준다. 전체 코드from collections import dequedx = [0,0,-1,1]dy = [-1,1,0,0]def bfs(x,y): queue = deque() queue.append((x,y)) visited = [[-1] * m for _ in range(n)] visited[x][y] = 0 while queue: x, y = queue.popleft() for i in range(4): nx = x + dx[i] ny = y + ..
2024. 9. 11.
[Algorithm] 백준 2665 미로만들기 | 파이썬 BFS
※ 2665 미로만들기https://www.acmicpc.net/problem/2665 문제 해결 TIPvisited 배열에 흰 방으로 바꾸어야 할 최소의 검은 방의 수를 저장한다. bfs로 탐색하면서, 흰 방인 경우를 먼저 탐색한다.(appendleft) 검은 방일 경우에는 이전 visited값에 1을 추가해준다. 전체 코드from collections import dequedx = [0,0,-1,1]dy = [-1,1,0,0]def bfs(x,y): queue = deque() queue.append((x,y)) visited = [[-1] * n for _ in range(n)] visited[x][y] = 0 while queue: x, y = queue.p..
2024. 9. 11.
[Algorithm] 백준 1926 그림 | 파이썬 BFS
※ 1926 그림https://www.acmicpc.net/problem/1926 문제 해결 TIPbfs로 간단하게 풀 수 있다. 유기농 배추 문제와 비슷하다. 전체 코드from collections import dequegraph = []n, m = map(int,input().split())for i in range(n): graph.append(list(map(int,input().split())))#graph = [[1, 1, 0, 1, 1], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [1, 0, 1, 1, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1]]dx = [-1,1,0,0]dy = [0,0,-1,1]def bfs(x,y): graph[x][..
2024. 7. 16.
[Algorithm] 백준 18405 경쟁적 전염 | 파이썬 BFS
※ 18405 경쟁적 전염https://www.acmicpc.net/problem/18405첫번째 문제 풀이 (시간 초과)문제를 보고 먼저 생각해낸 해결방법은1. 바이러스 번호 1번부터 graph에서 위치한 자리값을 position 배열에 저장한다.2. position 배열에서 하나씩 꺼내어 확산을 진행한다.N, K = map(int,input().split())graph = []for i in range(N): graph.append(list(map(int,input().split())))S, X, Y = map(int,input().split())#graph = [[1, 0, 2], [0, 0, 0], [3, 0, 0]]dx = [-1,1,0,0]dy = [0,0,-1,1]def bfs(x,y,..
2024. 7. 2.
[Algorithm] 백준 1012 유기농 배추 | 파이썬 BFS
※ 1012 유기농 배추https://www.acmicpc.net/problem/1012문제 해결 TIP처음에 DFS로 풀다가 RecursionError에 걸려서 BFS로 바꾸었다. 바이러스 문제와 비슷하게 상하좌우 주변 노드들을 방문하며 큐에 삽입하고 탐색하는 방법으로 진행한다. 여기서 주의할 점은 문제에서 가로 길이를 M, 세로 길이를 N으로 주어진 점이다. 전체 코드from collections import dequedx = [-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 rang..
2024. 6. 26.