diff --git "a/janghw0126/BFS/\354\240\201\353\241\235\354\203\211\354\225\275.py" "b/janghw0126/BFS/\354\240\201\353\241\235\354\203\211\354\225\275.py" new file mode 100644 index 0000000..e9f2667 --- /dev/null +++ "b/janghw0126/BFS/\354\240\201\353\241\235\354\203\211\354\225\275.py" @@ -0,0 +1,57 @@ +import sys +from collections import deque + +input = sys.stdin.readline + +def bfs(x, y, graph, visited): + q = deque() + q.append((x, y)) + visited[x][y] = True + + # 상하좌우 방향 선언하기 + dx, dy = [1, -1, 0, 0], [0, 0, 1, -1] + + while q: + x, y = q.popleft() + + for i in range(4): + nx, ny = x + dx[i], y + dy[i] + + if 0 <= nx < n and 0 <= ny < n and not visited[nx][ny]: + # 색상이 같고 방문하지 않은 경우에만 큐에 넣음 + if graph[x][y] == graph[nx][ny]: + visited[nx][ny] = True + q.append((nx, ny)) + +n = int(input()) +graph = [list(input().strip()) for _ in range(n)] + +# 첫 번째 방문 배열은 정상인 (R, G, B 구분함) +visited = [[False] * n for _ in range(n)] +normal_count = 0 + +# 두 번째 방문 배열은 적록색맹인 (R, G 구분 안 함) +visited_rg = [[False] * n for _ in range(n)] +rg_count = 0 + +# 정상인 그룹 개수 구하기 +for i in range(n): + for j in range(n): + if not visited[i][j]: + bfs(i, j, graph, visited) + normal_count += 1 + +# 적록색맹인 경우 'G'를 'R'로 바꾸기 +for i in range(n): + for j in range(n): + if graph[i][j] == 'G': + graph[i][j] = 'R' + +# 적록색맹인 그룹 개수 구하기 +for i in range(n): + for j in range(n): + if not visited_rg[i][j]: + bfs(i, j, graph, visited_rg) + rg_count += 1 + +print(normal_count, rg_count) \ No newline at end of file diff --git "a/janghw0126/BFS/\355\206\240\353\247\210\355\206\240.py" "b/janghw0126/BFS/\355\206\240\353\247\210\355\206\240.py" new file mode 100644 index 0000000..f40b39e --- /dev/null +++ "b/janghw0126/BFS/\355\206\240\353\247\210\355\206\240.py" @@ -0,0 +1,40 @@ +from collections import deque +import sys +input = sys.stdin.readline + +# 테스트 케이스를 입력받음 +m, n = map(int, input().split()) +tomato_grid = [list(map(int, input().split())) for _ in range(n)] + +# 익은 토마토 좌표를 큐에 추가함 +queue = deque([(i, j) for i in range(n) for j in range(m) if tomato_grid[i][j] == 1]) + +# 방향 벡터를 초기화함 (상, 하, 좌, 우) +directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] + +# BFS 탐색을 시작함 +def bfs(): + while queue: + x, y = queue.popleft() + for dx, dy in directions: + nx, ny = x + dx, y + dy + # 범위 내에 있고, 익지 않은 토마토(0)일 경우 익힘 + if 0 <= nx < n and 0 <= ny < m and tomato_grid[nx][ny] == 0: + tomato_grid[nx][ny] = tomato_grid[x][y] + 1 + queue.append((nx, ny)) + +# BFS를 실행함 +bfs() + +# 결과를 계산함 +days = 0 +for row in tomato_grid: + # 익지 않은 토마토가 있으면 -1을 출력함 + if 0 in row: + print(-1) + exit() + # 가장 큰 값이 걸린 날짜를 계산함 + days = max(days, max(row)) + +# 시작을 1에서 했으므로 결과에서 1을 빼줌 +print(days - 1) \ No newline at end of file diff --git a/janghw0126/README.md b/janghw0126/README.md index 6faff16..fe5bb05 100644 --- a/janghw0126/README.md +++ b/janghw0126/README.md @@ -1,5 +1,39 @@ -## ✏️ 기록 +## ✏️ 기록 +### 2024-1 +| 차시 | 날짜 | 문제유형 | 링크 | 풀이 | +|:----:|:---------:|:----:|:-----:|:----:| +| 1차시 | 2024.1.1 | 해시 | 완주하지 못한 선수 |[#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/4) | +| 2차시 | 2024.1.4 | 구현 | 단어의 개수 |[#9](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/9) | +| 3차시 | 2024.1.7 | 구현 | 단어 공부 |[#13](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/13) | +| 4차시 | 2024.1.10 | 구현 | 달팽이는 올라가고 싶다 |[#20](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/20) | +| 5차시 | 2024.1.13 | 수학 | 소수 |[#24](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/24) | +| 6차시 | 2024.1.16 | 스택 | 스택 2 |[#26](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/26) | +| 7차시 | 2024.1.19 | 스택 | 괄호 |[#30](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/30) | +| 8차시 | 2024.1.23 | 스택 | 스택 수열 |[#36](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/36) | +| 9차시 | 2024.1.25 | 스택 | 균형잡힌 세상 |[#40](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/40) | +| 10차시 | 2024.1.28 | 큐 | |[#42](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/42) | +| 11차시 | 2024.2.1 | 큐 | 프린터 큐 |[#48](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/48) | +| 12차시 | 2024.2.4 | DFS | 빵집 |[#53](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/53) | +| 13차시 | 2024.2.7 | DFS | 바이러스 |[#57](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/57) | +| 14차시 | 2024.2.12 | 백트래킹 | N과 M (1) |[#61](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/61) | +| 15차시 | 2024.2.15 | 백트래킹 | N과 M (2) |[#65](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/65) | +| 16차시 | 2024.2.18 | 백트래킹 | N과 M (5) |[#66](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/66) | +| 17차시 | 2024.2.21 | 백트래킹 | N과 M (3) |[#72](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/72) | +| 18차시 | 2024.2.24 | 그리디 | 잃어버린 괄호 |[#77](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/77) | +| 19차시 | 2024.2.27 | 이진 탐색 | 선분 위의 점 |[#79](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/79) | +| 20차시 | 2024.3.2 | 이진 탐색 | 수 찾기 |[#85](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/85) | +| 21차시 | 2024.3.5 | 정수론 | 약수의 합 |[#89](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/89) | +| 22차시 | 2024.3.8 | 분할 정복 | 종이의 개수 |[#91](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/91) | +| 23차시 | 2024.3.13 | 백트래킹 | 로또 |[#97](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/97) | +| 24차시 | 2024.3.15 | 누적합 | 출석체크 |[#101](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/101) | +| 25차시 | 2024.3.26 | 다익스트라 | 녹색 옷 입은 애가 젤다지? |[#108](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/108) | +| 26차시 | 2024.3.30 | 브루트 포스 | 퇴사 |[#110](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/110) | +| 27차시 | 2024.4.3 | 브루트 포스 | 에너지 모으기 |[#115](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/115) | +| 28차시 | 2024.4.12 | 정렬 | 수 정렬하기 2 |[#117](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/117) | +--- + +### 2024-2 | 차시 | 날짜 | 문제유형 | 링크 | 풀이 | |:----:|:---------:|:----:|:-----:|:----:| | 1차시 | 2024.7.17 | 수학 | 머리 톡톡 |[#124](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/124) | @@ -10,4 +44,10 @@ | 6차시 | 2024.8.3 | BFS | 쉬운 최단거리 |[#140](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/140) | | 7차시 | 2024.8.8 | DFS | 양과 늑대 |[#145](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/145) | | 8차시 | 2024.8.19 | 최소 힙 | N번째 큰 수 |[#145](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/147) | -| 9차시 | 2024.8.25 | BFS | 숨바꼭질 |[#149](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/149) | \ No newline at end of file +| 9차시 | 2024.8.25 | BFS | 숨바꼭질 |[#149](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/149) | +| 10차시 | 2024.9.9 | 위상정렬 | 줄 세우기 |[#158](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/158) | +| 11차시 | 2024.9.17 | 우선순위 큐 | 이중 우선순위 큐 |[#160](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/160) | +| 12차시 | 2024.9.24 | 투 포인터 | IOIOI |[#163](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/163) | +| 13차시 | 2024.9.27 | BFS | 토마토 |[#166](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/166) | +| 14차시 | 2024.10.5 | BFS | 적록색약 |[#172](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/172) | +--- diff --git "a/janghw0126/\354\234\204\354\203\201\354\240\225\353\240\254/\354\244\204 \354\204\270\354\232\260\352\270\260.py" "b/janghw0126/\354\234\204\354\203\201\354\240\225\353\240\254/\354\244\204 \354\204\270\354\232\260\352\270\260.py" new file mode 100644 index 0000000..072d8ce --- /dev/null +++ "b/janghw0126/\354\234\204\354\203\201\354\240\225\353\240\254/\354\244\204 \354\204\270\354\232\260\352\270\260.py" @@ -0,0 +1,39 @@ +import sys +from collections import deque + +n, m = map(int, sys.stdin.readline().split()) + +# 그래프와 진입 차수 리스트를 초기화함 +graph = [[] for _ in range(n + 1)] +deg = [0] * (n + 1) +q = deque() +res = [] + +# 비교 관계 입력과 그래프를 구성함 +for _ in range(m): + u, v = map(int, sys.stdin.readline().rstrip().split()) + # u가 v 앞에 서야 함 + graph[u].append(v) + # v의 진입 차수를 증가시킴 + deg[v] += 1 + +# 진입 차수가 0인 노드 큐에 삽입함 +for i in range(1, n + 1): + if deg[i] == 0: + q.append(i) + +# 위상 정렬을 시작함 +while q: + # 진입 차수가 0인 노드를 큐에서 꺼냄 + cur = q.popleft() + # 결과 리스트에 추가함 + res.append(cur) + + # 현재 노드 뒤에 서야 하는 노드들의 진입 차수를 감소시킴 + for next_node in graph[cur]: + deg[next_node] -= 1 + # 진입 차수가 0이 되면 큐에 추가함 + if deg[next_node] == 0: + q.append(next_node) + +print(*res) \ No newline at end of file diff --git "a/janghw0126/\355\210\254 \355\217\254\354\235\270\355\204\260/IOIOI.py" "b/janghw0126/\355\210\254 \355\217\254\354\235\270\355\204\260/IOIOI.py" new file mode 100644 index 0000000..7ec3866 --- /dev/null +++ "b/janghw0126/\355\210\254 \355\217\254\354\235\270\355\204\260/IOIOI.py" @@ -0,0 +1,32 @@ +import sys +input = sys.stdin.readline + +n = int(input()) +m = int(input()) +sequence = input().rstrip() + +# 투 포인터 알고리즘을 위해서 좌우 포인터 선언 +start, current = 0, 0 +# 'IOI' 패턴을 찾은 횟수 선언 +pattern_count = 0 + +# 문자열 범위 내에서 반복 +while current < m: + # 현재 위치에서 'IOI' 패턴이 발견된 경우 + if sequence[current:current + 3] == 'IOI': + # 패턴을 찾으면 포인터를 두 칸씩 이동 + current += 2 + # 패턴의 길이가 'N'에 맞는 경우 + if current - start == 2 * n: + # 패턴 카운트 증가 + pattern_count += 1 + # 패턴을 완성했으니 시작 포인터도 두 칸 이동 + start += 2 + else: + # 패턴이 맞지 않으면 한 칸씩 이동 + current += 1 + # 시작 포인터를 현재 포인터 위치로 재설정 + start = current + +# 패턴 횟수 출력 +print(pattern_count) \ No newline at end of file diff --git "a/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" "b/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" new file mode 100644 index 0000000..5d7a7df --- /dev/null +++ "b/janghw0126/\355\236\231/\354\235\264\354\244\221 \354\232\260\354\204\240\354\210\234\354\234\204 \355\201\220.py" @@ -0,0 +1,42 @@ +import sys +import heapq + +def solve(): + input = sys.stdin.readline + T = int(input()) + + for _ in range(T): + min_heap = [] # 최소 힙 + max_heap = [] # 최대 힙 (음수로 변환하여 저장) + count = int(input()) # 명령어 수 + status = [1] * count # 작업 상태 추적 (1: 유효, 0: 삭제됨) + + for i in range(count): + command, value = input().split() + value = int(value) + + if command == "I": # 삽입 연산 + heapq.heappush(min_heap, (value, i)) + heapq.heappush(max_heap, (-value, i)) + elif command == "D": # 삭제 연산 + if value == -1: # 최소값 삭제 + if min_heap: + status[heapq.heappop(min_heap)[1]] = 0 + elif value == 1: # 최대값 삭제 + if max_heap: + status[heapq.heappop(max_heap)[1]] = 0 + + # 유효하지 않은 값 제거 + while min_heap and status[min_heap[0][1]] == 0: + heapq.heappop(min_heap) + while max_heap and status[max_heap[0][1]] == 0: + heapq.heappop(max_heap) + + # 결과 출력 + if not min_heap or not max_heap: + print("EMPTY") + else: + print(-max_heap[0][0], min_heap[0][0]) + +if __name__ == "__main__": + solve() \ No newline at end of file