Skip to content

Commit

Permalink
Merge branch 'main' into 12-H0ngJu
Browse files Browse the repository at this point in the history
  • Loading branch information
H0ngJu authored May 6, 2024
2 parents 4f2026e + 3fe0703 commit 5a653be
Show file tree
Hide file tree
Showing 22 changed files with 734 additions and 17 deletions.
33 changes: 33 additions & 0 deletions H0ngJu/BFS/숨바꼭질4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import sys
from collections import deque

def input(): return sys.stdin.readline()

N, K = map(int, input().split())

visited = [0] * 100001
time = 0
parent = [-1] * 100001

q = deque([N])
visited[N] = 1

while q:
size = len(q)
for _ in range(size):
location = q.popleft()
if location == K:
print(time)
answer = []
while location != -1:
answer.append(location)
location = parent[location]
answer.reverse()
print(" ".join(map(str, answer)))
break
for next_location in (location + 1, location - 1, location * 2):
if 0 <= next_location <= 100000 and not visited[next_location]:
q.append(next_location)
visited[next_location] = 1
parent[next_location] = location
time += 1
21 changes: 21 additions & 0 deletions H0ngJu/DP/RGB거리 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import sys

def input(): return sys.stdin.readline().rstrip()

N = int(input())
INF = 1000*1000+1

house = [[int(x) for x in input().split()] for _ in range(N)]
cost = INF

for first_house in range(3):
dp = [[INF] * 3 for _ in range(N)]
dp[0][first_house] = house[0][first_house]
for i in range(1, N):
dp[i][0] = min(dp[i-1][1], dp[i-1][2]) + house[i][0]
dp[i][1] = min(dp[i-1][0], dp[i-1][2]) + house[i][1]
dp[i][2] = min(dp[i-1][0], dp[i-1][1]) + house[i][2]
dp[N-1][first_house] = INF
cost = min(cost, min(dp[N-1]))

print(cost)
4 changes: 4 additions & 0 deletions H0ngJu/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
| 5차시 | 2024.03.16 | 구현 | [요세푸스 문제](https://www.acmicpc.net/problem/1158) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/161 |
| 6차시 | 2024.03.19 | 스택 | [오큰수](https://www.acmicpc.net/problem/17298) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/164 |
| 7차시 | 2024.03.22 | DP | [1,2,3 더하기](https://www.acmicpc.net/problem/9095) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/166 |
| 8차시 | 2024.03.16 | DP | [쉬운 계단 수](https://www.acmicpc.net/problem/10844) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/170 |
| 9차시 | 2024.03.22 | DP | [RGB거리 2](https://www.acmicpc.net/problem/17404) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/172 |
| 10차시 | 2024.04.03 | BFS | [숨바꼭질 4](https://www.acmicpc.net/problem/13913) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/177 |
| 11차시 | 2024.04.07 | 구현 | [사탕 게임](https://www.acmicpc.net/problem/9095) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/178 |
| 12차시 | 2024.04.09 | DFS | [ABCDE](https://www.acmicpc.net/problem/13023) | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/180 |

---
44 changes: 44 additions & 0 deletions H0ngJu/구현/사탕 게임.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import sys

def input(): return sys.stdin.readline().rstrip()

def eat(row, col):
row_cnt = 0
col_cnt = 0
tmp_col = 1
tmp_row = 1
for idx in range(N-1):
# 열에서 먹을 때
if data[row][idx] == data[row][idx+1]:
tmp_col += 1
col_cnt = max(col_cnt, tmp_col)
else:
tmp_col = 1

# 행에서 먹을 때
if data[idx][col] == data[idx+1][col]:
tmp_row += 1
row_cnt = max(row_cnt, tmp_row)
else:
tmp_row = 1

return max(row_cnt, col_cnt)


N = int(input())
data = [[x for x in input()] for _ in range(N)]
directions = [(0,-1), (0,1), (-1,0),(1,0)]
cnt = 0

for i in range(N):
for k in range(N):
for dir in directions:
dx, dy = dir
x = i+dx
y = k+dy
if 0<=x<N and 0<=y<N:
data[i][k], data[x][y] = data[x][y], data[i][k]
cnt = max(cnt, eat(x,y))
data[i][k], data[x][y] = data[x][y], data[i][k] #다시 되돌리기

print(cnt)
28 changes: 28 additions & 0 deletions H0ngJu/쉬운 계단 수.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import sys

DIV = 1_000_000_000

N = int(sys.stdin.readline().rstrip())

dp = [[0] * 10 for _ in range(N+1)]
answer = 0

for i in range(1,N+1):
if i == 1:
for k in range(1,10):
dp[i][k] = 1

else:
for num in range(10):
if num == 0:
dp[i][num] = dp[i-1][1]%DIV
elif num == 9:
dp[i][num] = dp[i-1][8]%DIV
else:
dp[i][num] = (dp[i-1][num-1] + dp[i-1][num+1])%DIV

for k in range(10):
answer += dp[N][k]

print(answer%DIV)

File renamed without changes.
3 changes: 2 additions & 1 deletion Munbin-Lee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
| 35차시 | 2024.02.18 | 백트래킹 | <a href="https://www.acmicpc.net/problem/24891">단어 마방진</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/140 |
| 36차시 | 2024.02.21 | 문자열 | <a href="https://www.acmicpc.net/problem/15927">회문은 회문아니야!!</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143 |
| 37차시 | 2024.03.05 | 백트래킹 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/250136">석유 시추</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/150 |
| 37차시 | 2024.03.08 | 트라이 | <a href="https://www.acmicpc.net/problem/5052">전화번호 목록</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
| 38차시 | 2024.03.08 | 트라이 | <a href="https://www.acmicpc.net/problem/5052">전화번호 목록</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/155 |
| 39차시 | 2024.03.14 | 문자열 | <a href="https://www.acmicpc.net/problem/1050">물약</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/160 |
---
45 changes: 45 additions & 0 deletions Munbin-Lee/문자열/39-물약.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
stdin = open(0)

def input():
return stdin.readline().rstrip()

N, M = map(int, input().split())

prices = {}

for _ in range(N):
item, price = input().split()
prices[item] = int(price)

recipes = []

for _ in range(M):
target, formula = input().split('=')
terms = formula.split('+')
recipes.append([target, terms])

def updatePrice(target, terms):
price = 0

for term in terms:
count = int(term[0])
item = term[1:]

if item not in prices:
return

price += count * prices[item]

if target not in prices or prices[target] > price:
prices[target] = price

for _ in range(M):
for recipe in recipes:
updatePrice(recipe[0], recipe[1])

if 'LOVE' not in prices:
print('-1')
exit()

answer = min(prices['LOVE'], 1000000001)
print(answer)
73 changes: 73 additions & 0 deletions pknujsp/BFS/42-다리 만들기.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from collections import *

map_size = int(input())
matrix = [list(map(int, input().split())) for _ in range(map_size)]

directions = [(1, 0), (0, -1), (-1, 0), (0, 1)]
island_id = 2
costs = [[0] * map_size for _ in range(map_size)]

# 다리를 놓을 위치를 저장할 큐
bridge_queue = deque()

# 전체 맵을 순회하면서 각 섬에 ID 부여
for row in range(map_size):
for col in range(map_size):

if matrix[row][col] != 1:
continue

# 현재 칸을 시작으로 하는 섬에 고유 ID 할당
matrix[row][col] = island_id
queue = deque([(row, col)])

# BFS를 통해 같은 섬인 육지에 ID 할당
while queue:
r, c = queue.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc

if not 0 <= nr < map_size or not 0 <= nc < map_size:
continue

# 다른 섬과 연결된 바다 영역에 접근하면 종료
if 1 < matrix[nr][nc] < island_id:
print(1)
exit()

# 새로운 육지 발견 시 큐에 추가
if matrix[nr][nc] == 1:
queue.append((nr, nc))
# 육지와 바로 맞닿은 바다 영역을 다리 후보로 추가
elif matrix[nr][nc] == costs[nr][nc] == 0:
bridge_queue.append((nr, nc, 1, island_id))

costs[nr][nc] = 1
matrix[nr][nc] = island_id

island_id += 1

min_cost = int(1e6)

# 다리 후보 위치를 순회하며 최소 다리 길이 계산
while bridge_queue:
r, c, cost, curr_island_id = bridge_queue.popleft()

for dr, dc in directions:
nr, nc = r + dr, c + dc

if not 0 <= nr < map_size or not 0 <= nc < map_size:
continue

# 아직 다리가 놓이지 않은 바다 영역이면
# 다리 길이를 증가시키고 큐에 추가
if costs[nr][nc] == 0:
bridge_queue.append((nr, nc, cost + 1, curr_island_id))

costs[nr][nc] = cost + 1
matrix[nr][nc] = curr_island_id
# 다른 섬과 연결된 다리 영역에 접근하였다면 최소 비용을 갱신
elif matrix[nr][nc] > 1 and matrix[nr][nc] != curr_island_id:
min_cost = min(min_cost, cost + costs[nr][nc])

print(min_cost)
50 changes: 50 additions & 0 deletions pknujsp/BRUTE_FORCE/38-자물쇠와 열쇠.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 자물쇠의 중간 부분이 모두 1인지 확인
def is_valid(new_lock):
length = len(new_lock) // 3

for r in range(length, length * 2):
for c in range(length, length * 2):
if new_lock[r][c] != 1:
return False

return True

def solution(key, lock):
n = len(lock)
k = len(key)
new_lock = [[0] * (n * 3) for _ in range(n * 3)]

for r in range(n):
for c in range(n):
new_lock[r + n][c + n] = lock[r][c]

for _ in range(4):
rev_key = key[::-1]
key = []
for c in range(k):
row = []
for r in range(k):
row.append(rev_key[r][c])
key.append(row)

"""
열쇠를 돌리는 로직은 한 줄로도 구현가능 합니다
key = [row for row in zip(*reversed(key))]
"""

for r in range(n * 2):
for c in range(n * 2):
# 자물쇠에 열쇠를 끼운다
for i in range(k):
for j in range(k):
new_lock[r + i][c + j] += key[i][j]

# 자물쇠에 열쇠가 딱 들어갔는지 확인
if is_valid(new_lock):
return True

# 자물쇠에서 열쇠를 빼서 복구시킨다
for i in range(k):
for j in range(k):
new_lock[r + i][c + j] -= key[i][j]
return False
8 changes: 7 additions & 1 deletion pknujsp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@
| 34차시 | 2024.02.12 | BFS | [이분 그래프](https://www.acmicpc.net/problem/1707) | [#135](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/135) |
| 35차시 | 2024.02.18 | 그리디 | [선물할인](https://www.acmicpc.net/problem/25947) | [#137](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137) |
| 36차시 | 2024.02.21 | 이진탐색 | [휴게소 세우기](https://www.acmicpc.net/problem/1477) | [#143](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/143) |
| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) |
| 37차시 | 2024.03.04 | 구현 | [n+1 카드게임](https://school.programmers.co.kr/learn/courses/30/lessons/258707) | [#149](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/149) |
| 38차시 | 2024.03.08 | BRUTE_FORCE | [자물쇠와 열쇠](https://school.programmers.co.kr/learn/courses/30/lessons/60059) | [#154](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/154) |
| 39차시 | 2024.03.19 || [디스크 컨트롤러](https://school.programmers.co.kr/learn/courses/30/lessons/42627) | [#163](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/163) |
| 40차시 | 2024.03.22 | 그리디 | [센서](https://www.acmicpc.net/problem/2212) | [#168](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/168) |
| 41차시 | 2024.03.25 | 다익스트라 | [알고스팟](https://www.acmicpc.net/problem/1261) | [#169](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/169) |
| 42차시 | 2024.03.29 | BFS | [다리 만들기](https://www.acmicpc.net/problem/2146) | [#173](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/173) |
| 43차시 | 2024.04.03 | 그리디 | [저울](https://www.acmicpc.net/problem/2437) | [#175](https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/175) |
18 changes: 18 additions & 0 deletions pknujsp/그리디/40-센서.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sys import *

N = int(stdin.readline())
K = int(stdin.readline())
SENSORS = sorted(set(map(int, stdin.readline().split())))

if K >= N:
print(0)
exit()

distances = [SENSORS[i] - SENSORS[i - 1] for i in range(1, len(SENSORS))]
distances.sort(reverse=True)

result = 0
for i in range(K - 1, len(distances)):
result += distances[i]

print(result)
13 changes: 13 additions & 0 deletions pknujsp/그리디/43-저울.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
N = int(input())
weights = list(map(int, input().split()))
weights.sort()

x = 0
for weight in weights:
# x + 1이 현재 추의 무게보다 작다면, 측정이 불가능함
if x + 1 < weight:
break

x += weight

print(x + 1)
Loading

0 comments on commit 5a653be

Please sign in to comment.