Skip to content

Commit 0327057

Browse files
authored
Merge pull request #100 from AlgorithmStudy-Allumbus/minjeong
Minjeong / 12월 1주차 / 5문제
2 parents c3341b4 + 66e86d7 commit 0327057

File tree

5 files changed

+268
-0
lines changed

5 files changed

+268
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# 게임판 그래프 정의
2+
graph = [[1], [2], [3], [4], [5],
3+
[6, 21], [7], [8], [9], [10],
4+
[11, 25], [12], [13], [14], [15],
5+
[16, 27], [17], [18], [19], [20],
6+
[32], [22], [23], [24], [30],
7+
[26], [24], [28], [29], [24],
8+
[31], [20], [32]]
9+
10+
# 각 칸의 점수
11+
score = [0, 2, 4, 6, 8,
12+
10, 12, 14, 16, 18,
13+
20, 22, 24, 26, 28,
14+
30, 32, 34, 36, 38,
15+
40, 13, 16, 19, 25,
16+
22, 24, 28, 27, 26,
17+
30, 35, 0]
18+
19+
dice = list(map(int, input().split())) # 주사위 값 입력
20+
answer = 0 # 최대 점수 저장 변수
21+
22+
# 백트래킹 함수
23+
def backtracking(depth, result, horses):
24+
global answer
25+
# 10개의 주사위를 모두 사용한 경우 최대값 갱신
26+
if depth == 10:
27+
answer = max(answer, result)
28+
return
29+
30+
# 4개의 말을 순서대로 선택해 이동
31+
for i in range(4):
32+
x = horses[i] # 현재 말 위치
33+
34+
# 파란색 화살표 처리
35+
if len(graph[x]) == 2:
36+
x = graph[x][1]
37+
else:
38+
x = graph[x][0]
39+
40+
# 주사위 값만큼 이동
41+
for _ in range(1, dice[depth]):
42+
x = graph[x][0]
43+
44+
# 이동한 위치가 도착 칸이거나, 다른 말이 없는 칸일 때만 이동
45+
if x == 32 or (x < 32 and x not in horses):
46+
before = horses[i] # 원래 위치 저장
47+
horses[i] = x # 말 이동
48+
backtracking(depth + 1, result + score[x], horses) # 재귀 호출
49+
horses[i] = before # 위치 복구
50+
51+
# 백트래킹 시작
52+
backtracking(0, 0, [0, 0, 0, 0])
53+
print(answer)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 테스트 케이스 입력
5+
T = int(input())
6+
7+
for _ in range(T):
8+
# 입력 처리
9+
K = int(input())
10+
files = [0] + list(map(int, input().split()))
11+
prefix_sum = [0] * (K + 1)
12+
13+
# 누적합 계산
14+
for i in range(1, K + 1):
15+
prefix_sum[i] = prefix_sum[i - 1] + files[i]
16+
17+
# DP 배열 초기화
18+
dp = [[0] * (K + 1) for _ in range(K + 1)]
19+
20+
# DP 계산
21+
for count in range(1, K): # 합치는 파일 개수
22+
for start in range(1, K - count + 1): # 시작 인덱스
23+
end = start + count
24+
MIN = sys.maxsize
25+
for mid in range(start, end): # 중간 분할점
26+
MIN = min(MIN, dp[start][mid] + dp[mid + 1][end])
27+
dp[start][end] = MIN + prefix_sum[end] - prefix_sum[start - 1]
28+
29+
# 결과 출력
30+
print(dp[1][K])
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
'''
6+
내용 정리하면서 최적화한 코드
7+
'''
8+
# 1. 입력
9+
N = int(input()) # 보드 크기
10+
K = int(input()) # 사과 개수
11+
12+
# 사과 위치 입력
13+
apples = set()
14+
for _ in range(K):
15+
x, y = map(int, input().split())
16+
apples.add((x - 1, y - 1)) # 0-index로 변환
17+
18+
L = int(input()) # 방향 변환 횟수
19+
turn_times = deque([]) # 방향 전환 정보 / [(3, 'D'), (15, 'L'), (17, 'D')]
20+
for _ in range(L):
21+
t, d = input().split()
22+
turn_times.append((int(t), d))
23+
24+
# 2. 초기화
25+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 방향 리스트 (우, 하, 좌, 상) # 방향 변환에 따라 인덱싱
26+
snake = deque([(0, 0)]) # 뱀 초기 위치: 큐
27+
current = 0 # 초기 방향. (오른쪽)
28+
time = 0
29+
30+
# 3. 시뮬레이션
31+
while True:
32+
time += 1
33+
34+
# 3.1. 뱀 머리 이동
35+
head_X, head_Y = snake[-1] # 머리 좌표
36+
dx, dy = directions[current] # 오른쪽 좌표
37+
new_head = (head_X + dx, head_Y + dy)
38+
39+
# 3.2. 벽이나 자기 몸에 닿았다면 게임을 종료
40+
if not (0 <= new_head[0] < N and 0 <= new_head[1] < N) or new_head in snake:
41+
print(time) # 게임 종료
42+
break
43+
44+
# 3.3. 사과 확인 하고 꼬리를 처리
45+
snake.append(new_head) # 새로운 머리 추가
46+
47+
# 사과 있는지 확인
48+
if new_head in apples:
49+
apples.remove(new_head) # 사과 제거
50+
else:
51+
snake.popleft() # 꼬리 제거
52+
53+
# 3.4. 방향 전환
54+
if turn_times and time == turn_times[0][0]:
55+
_, direction = turn_times.popleft()
56+
if direction == 'D': # 오른쪽
57+
current = (current + 1) % 4
58+
elif direction == 'L': # 왼쪽
59+
current = (current + 3) % 4
60+
61+
'''
62+
# 문제 푸는 당시에 작성했던 코드
63+
import sys
64+
from collections import deque
65+
66+
input = sys.stdin.readline
67+
68+
N = int(input()) # 보드 크기
69+
K = int(input()) # 사과 개수
70+
board = [[0] * N for _ in range(N)] # 보드 판
71+
72+
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)] # 방향 리스트 (우, 하, 좌, 상) # 방향 변환에 따라 인덱싱
73+
snake = deque([(0, 0)]) # 뱀 초기 위치: 큐
74+
current = 0 # 현재 방향은 오른쪽
75+
time = 0
76+
direction_info = deque([]) # 방향 정보 / [(3, 'D'), (15, 'L'), (17, 'D')]
77+
78+
# 사과 위치 입력, 배치
79+
for _ in range(K):
80+
x, y = map(int, input().split())
81+
board[x - 1][y - 1] = 1
82+
83+
L = int(input()) # 방향 변환 횟수
84+
for _ in range(L):
85+
t, d = map(str, input().split())
86+
direction_info.append((int(t), d))
87+
88+
89+
while True:
90+
time += 1
91+
92+
# 뱀 머리 이동
93+
head_X, head_Y = snake[-1] # 머리 좌표
94+
dx, dy = directions[current] # 오른쪽 좌표
95+
new_head = (head_X + dx, head_Y + dy)
96+
97+
# 벽이나 자기 몸에 닿았는지 판단한다.
98+
if (0 <= new_head[0] < N and 0 <= new_head[1] < N) and new_head not in snake:
99+
snake.append(new_head) # 새로운 머리 추가
100+
101+
# 사과 있는지 확인
102+
# 있으면 꼬리 냅두고,
103+
if board[new_head[0]][new_head[1]] == 1:
104+
board[new_head[0]][new_head[1]] = 0 # 사과 먹음
105+
106+
# 없으면 꼬리를 줄인다.
107+
else:
108+
snake.popleft()
109+
110+
# 방향 바꾸기
111+
if direction_info and time == direction_info[0][0]:
112+
t, dir = direction_info.popleft()
113+
if dir == 'D': # 오른쪽
114+
current = (current + 1) % 4
115+
elif dir == 'L': # 왼쪽
116+
current = (current + 3) % 4
117+
118+
else:
119+
# 게임 종료
120+
print(time)
121+
break
122+
'''
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
# 1. 입력 처리
7+
n, w, L = map(int, input().split())
8+
trucks = list(map(int, input().split()))
9+
10+
# 2. 초기화
11+
bridge = deque() # 다리 위 트럭들: (트럭 무게, 다리에서 빠져나갈 시간)
12+
current_weight = 0 # 다리 위의 현재 총 무게
13+
time = 0 # 현재 시간
14+
truck_index = 0 # 대기 중인 트럭의 인덱스
15+
16+
# 3. 시뮬레이션 수행
17+
while truck_index < n or bridge:
18+
time += 1 # 시간 + 1
19+
20+
# 3.1 다리에서 빠져나갈 트럭 제거
21+
if bridge and bridge[0][1] == time: # 다리에서 나갈 시간인지 확인
22+
truck_weight, _ = bridge.popleft()
23+
current_weight -= truck_weight
24+
25+
# 3.2 새로운 트럭을 다리에 올릴 수 있는지 확인
26+
if truck_index < n and current_weight + trucks[truck_index] <= L:
27+
bridge.append((trucks[truck_index], time + w)) # 트럭 추가 (무게, 나갈 시간)
28+
current_weight += trucks[truck_index]
29+
truck_index += 1
30+
31+
# 4. 결과 출력
32+
print(time)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
from collections import deque
3+
4+
input = sys.stdin.readline
5+
6+
N = int(input()) # 앵무새 수
7+
S = [] # 앵무새 문장
8+
for _ in range(N):
9+
S.append(deque(input().split()))
10+
L = deque(input().split()) # 받아 적은 문장
11+
12+
while L:
13+
current = L.popleft() # 현재 단어
14+
found = False
15+
16+
# 각 앵무새 문장에서 단어 찾기
17+
for s in S:
18+
if s and s[0] == current: # 매칭되는 단어가 있다면
19+
s.popleft() # 단어 제거
20+
found = True
21+
break
22+
23+
if not found: # 매칭되는 단어가 없으면 Impossible
24+
print("Impossible")
25+
exit()
26+
27+
# 모든 문장이 비었는지 확인
28+
if any(s for s in S): # S에 남은 문장이 있다면 Impossible
29+
print("Impossible")
30+
else:
31+
print("Possible")

0 commit comments

Comments
 (0)