Algorithm/Baekjoon
-
[Python][C++] 5430번 ACAlgorithm/Baekjoon 2024. 8. 11. 16:11
문제https://www.acmicpc.net/problem/5430 코드1. Pythonimport sysfrom collections import dequeinput = sys.stdin.readlinefor _ in range(int(input())): try: s = input().rstrip() n = int(input()) idx = 1 # 배열의 순서를 관리하는 변수 (1이면 정방향, -1이면 역방향) nums = deque(input()[1:-2].split(',')) if nums[0] == '': # 만약 배열이 비어있는 경우 처리 (빈 배열의 경우) nums.p..
-
[Python][C++] 2805번 나무 자르기Algorithm/Baekjoon 2024. 8. 10. 22:30
문제https://www.acmicpc.net/problem/2805 코드1. Pythonn, m = map(int, input().split())trees = list(map(int, input().split()))start = 1 # 절단기 높이의 최소값end = max(trees) # 절단기 높이의 최대값 (가장 높은 나무의 높이)# 이분 탐색 시작while start mid: # 나무가 절단기 높이보다 크면 sum_wood += tree - mid # 잘려진 나무의 길이를 sum_wood에 더함 # 얻을 수 있는 나무의 총합이 필요한 나무의 길이 m 이상이면 if sum_wood >= m: start = mid + 1 # 절단기 높이를 더 높..
-
[Python][C++] 2667번 단지 번호 붙이기Algorithm/Baekjoon 2024. 8. 8. 19:11
문제https://www.acmicpc.net/problem/2667 코드1. Pythonimport sysinput = sys.stdin.readlinen = int(input())graph = []for _ in range(n): graph.append(input().rstrip())# 방향 벡터 (상, 하, 좌, 우)dx = [-1, 1, 0, 0]dy = [0, 0, -1, 1]def dfs(x, y): # 보드의 범위를 벗어나면 False 반환 if x = n or y = n: return False global cnt # 현재 위치가 집(1)인 경우 if graph[x][y] == '1': # 현재 위치를 방문했음을 표시 (1 -> 0..
-
[Python][C++] 2630번 색종이 만들기Algorithm/Baekjoon 2024. 8. 8. 15:34
문제https://www.acmicpc.net/problem/2630 코드1. Pythonimport sysinput = sys.stdin.readlinedef recursion(x, y, m): global white, blue # 현재 m x m 크기 종이 모든 칸 검사 for i in range(x, x + m): for j in range(y, y + m): # 다른 색이 섞여 있으면 종이 4등분해 재귀 호출 if board[i][j] != board[x][y]: recursion(x, y, m // 2) recursion(x, y + m // 2, m // 2) ..
-
[Python][C++] 2606번 바이러스Algorithm/Baekjoon 2024. 7. 12. 14:39
문제https://www.acmicpc.net/problem/2606 코드1. Pythonimport sysinput = sys.stdin.readlinedef bfs(V): global cnt q = [V] # 탐색을 시작할 노드 V를 큐에 넣음 visited[V] = 1 # 시작 노드를 방문 처리 while q: V = q.pop(0) # 큐에서 노드 하나를 꺼내고 cnt += 1 for i in range(1, n+1): # 모든 노드를 순회 if graph[V][i] == 1 and visited[i] == 0: # 현재 노드와 인접하고 아직 방문하지 않은 노드라면 ..
-
[Python][C++] 2579번 계단 오르기Algorithm/Baekjoon 2024. 7. 10. 21:33
문제https://www.acmicpc.net/problem/2579 코드1. Pythonn = int(input())stair = []for _ in range(n): stair.append(int(input()))if n == 1: print(stair[0]) returndp = [0] * ndp[0] = stair[0]dp[1] = stair[0] + stair[1]if n > 2: # 세 번째 계단까지의 최대 점수는 첫 번째와 세 번째 계단의 합 또는 두 번째와 세 번째 계단의 합 중 큰 값 dp[2] = max(stair[0] + stair[2], stair[1] + stair[2])for i in range(3, n): # 각 계단 i에서의 최대 점수는 (i-..
-
[Python][C++] 2178번 미로 탐색Algorithm/Baekjoon 2024. 7. 8. 23:21
문제https://www.acmicpc.net/problem/2178 코드1. Pythonimport sysfrom collections import dequeinput = sys.stdin.readlinedef bfs(x, y): graph[x][y] = 1 d = deque() d.append((x, y)) while d: x, y = d.popleft() # 현재 위치를 덱에서 꺼냄 for i in range(4): # 네 방향으로 탐색 nx = x + dx[i] ny = y + dy[i] # 새로운 위치가 보드 범위 밖이면 무시 if nx = n or ny = ..
-
[Python][C++] 1931번 회의실 배정Algorithm/Baekjoon 2024. 7. 7. 18:03
문제https://www.acmicpc.net/problem/1931 코드1. Pythonimport sysinput = sys.stdin.readlineN = int(input())time = [[0]*2 for _ in range(N)]for i in range(N): s, e = map(int, input().split()) time[i] = [s, e]# 회의의 종료 시간을 기준으로, 종료 시간이 같으면 시작 시간을 기준으로 정렬time.sort(key=lambda x: (x[1], x[0]))cnt = 1 # 선택된 회의의 수 (첫 번째 회의는 무조건 선택됨)end_time = time[0][1] # 첫 번째 회의의 종료 시간# 두 번째 회의부터 마지막 회의까지 순회for i i..