Algorithm
-
[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..
-
[Python][C++] 1927번 최소 힙Algorithm/Baekjoon 2024. 7. 7. 16:16
문제https://www.acmicpc.net/problem/1927 코드1. Pythonimport sysimport heapqinput = sys.stdin.readlineheap = []for _ in range(int(input())): x = int(input()) if x: # x가 0이 아니면 힙에 x를 추가 heapq.heappush(heap, x) else: # x가 0이면 힙에서 최솟값을 제거하고 출력 # 만약 힙이 비어있으면 0을 출력 print(heapq.heappop(heap) if heap else 0) 2. C++#include #include #include using namespace std;i..
-
[Python][C++] 1764번 듣보잡Algorithm/Baekjoon 2024. 7. 6. 21:29
문제https://www.acmicpc.net/problem/1764 코드1. Pythonimport sys# 입력을 빠르게 받기 위해 사용input = sys.stdin.readline# 듣도 못한 사람의 수 n과 보도 못한 사람의 수 m 입력n, m = map(int, input().split())# 듣도 못한 사람의 이름을 저장할 집합 s1s1 = set()# 보도 못한 사람의 이름을 저장할 집합 s2s2 = set()for _ in range(n): s1.add(input().strip()) for _ in range(m): s2.add(input().strip())# 두 집합의 교집합을 구하고 정렬하여 리스트로 변환s = sorted(s1 & s2)print(len(s))for..
-
[Python][C++] 1697번 숨바꼭질Algorithm/Baekjoon 2024. 7. 6. 20:49
문제https://www.acmicpc.net/problem/1697 코드1. Pythonfrom collections import dequedef bfs(): q = deque() q.append(n) # 시작점 n을 큐에 추가 while q: # 큐가 비어있지 않을 동안 반복 x = q.popleft() # 큐의 첫 번째 요소를 꺼냄 if x == k: # 현재 위치가 동생의 위치(k)와 같다면 print(dist[x]) # 이동 횟수를 출력 break # 탐색을 종료 for nx in (x - 1, x + 1, x * 2): # 이동 가능한 위치들에 대해 반복 if 0 2..
-
[Python][C++] 1620번 나는야 포켓몬 마스터 이다솜Algorithm/Baekjoon 2024. 7. 5. 23:15
문제https://www.acmicpc.net/problem/1620 코드1. Pythonimport sysinput = sys.stdin.readlinen, m = map(int, input().split())dic = {}# 'n'개의 이름을 입력받아 딕셔너리에 추가하는 루프for i in range(1, n + 1): a = input().rstrip() # 이름을 입력받아 공백을 제거한 후 변수 'a'에 저장 dic[a] = str(i) # 이름을 키로, 번호 'i'를 문자열로 변환하여 값으로 딕셔너리에 저장 dic[str(i)] = a # 번호 'i'를 문자열로 변환하여 키로, 이름을 값으로 딕셔너리에 저장# 'm'개의 쿼리를 처리하는 루프for _ in range(m..