Algorithm
-
[Python][C++]7662번 우선순위 큐Algorithm/Baekjoon 2024. 8. 19. 21:05
문제https://www.acmicpc.net/problem/7662 코드1. Pythonimport sysimport heapqinput = sys.stdin.readline # 빠른 입력을 위해 sys.stdin.readline 사용# 테스트 케이스 수만큼 반복for _ in range(int(input())): min_heap = [] # 최소 힙 (최소값을 빠르게 꺼내기 위함) max_heap = [] # 최대 힙 (최대값을 빠르게 꺼내기 위함, 음수로 저장하여 사용) visited = {} # 각 숫자가 힙에 몇 번 들어갔는지 추적하기 위한 딕셔너리 # 각 테스트 케이스의 명령어 수만큼 반복 for _ in range(int(input())): ..
-
[Python][C++] 7576번 토마토Algorithm/Baekjoon 2024. 8. 18. 01:09
문제https://www.acmicpc.net/problem/7576 코드1. Pythonfrom collections import deque# m과 n을 입력받음 (m: 가로 길이, n: 세로 길이)m, n = map(int, input().split())# 보드 초기화board = []# 보드 상태를 입력받아 2차원 리스트로 저장for _ in range(n): board.append(list(map(int, input().split())))# 방향 벡터 설정 (상, 하, 좌, 우)dx = [-1, 1, 0, 0]dy = [0, 0, -1, 1]# BFS를 위한 큐 초기화q = deque()# 익은 토마토(값이 1인 칸)의 위치를 모두 큐에 추가for i in range(n): for ..
-
[Python][C++] 7569번 토마토Algorithm/Baekjoon 2024. 8. 16. 22:58
문제https://www.acmicpc.net/problem/7569 코드1. Pythonfrom collections import deque# 보드와 방문 여부를 저장하는 배열 초기화board = [[[0 for _ in range(100)] for _ in range(100)] for _ in range(100)]visited = [[[0 for _ in range(100)] for _ in range(100)] for _ in range(100)]def bfs(n, m, h): # 방향벡터 설정 (6방향) dx = [-1, 1, 0, 0, 0, 0] dy = [0, 0, -1, 1, 0, 0] dz = [0, 0, 0, 0, -1, 1] q = deque() # 익..
-
[Python][C++] 5525번 IOIOIAlgorithm/Baekjoon 2024. 8. 14. 21:43
문제https://www.acmicpc.net/problem/5525 코드1. Python# 입력 받기n = int(input())m = int(input())s = input()cnt = 0 # 패턴이 나타나는 횟수를 저장할 변수i = 0 # 현재 문자열의 인덱스# 문자열을 순회하면서 패턴을 찾기 위한 반복문while i = n: cnt += k - n + 1 # 가능한 패턴의 시작점 수만큼 카운트를 증가시킴 else: i += 1 # "IOI" 패턴이 아닐 경우 다음 인덱스로 이동# 최종적으로 패턴이 몇 번 나타났는지 출력print(cnt) 2. C++#include #include using namespace std;int main() { int ..
-
[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) ..