99클럽 코테 스터디 10일차 TIL + 백준 2573 빙산 (파이썬)
※ 2573 빙산https://www.acmicpc.net/problem/2573문제 해결 TIP기존 bfs 알고리즘으로 탐색과 녹이기를 동시에 수행할 수 있다. 전체 코드 from collections import dequeimport sysinput = sys.stdin.readlinedx = [-1,0,1,0]dy = [0,1,0,-1]N, M = map(int,input().split())graph = []year = 0for i in range(N): graph.append(list(map(int,input().split())))def bfs(x,y): queue = deque() queue.append((x,y)) visited[x][y] = 1 while queu..
2025. 1. 25.
[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.