Skip to content

Commit ac928b0

Browse files
committed
Update
1 parent fe6d3a3 commit ac928b0

File tree

15 files changed

+418
-0
lines changed

15 files changed

+418
-0
lines changed

Diff for: Exams/solutions/A_1.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
n = int(input())
2+
a = list(map(int, input().split()))
3+
a.sort()
4+
5+
# Median(가운데) 값을 출력합니다.
6+
print(a[(n - 1) // 2])

Diff for: Exams/solutions/A_2.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
n, k = map(int, input().split())
2+
a = list(map(int, input().split()))
3+
result = 0
4+
5+
def dfs(x):
6+
global result
7+
# n보다 작거나 같은 자연수에 대해서만 확인
8+
if x > n:
9+
return
10+
# K의 원소로만 구성된 가장 큰 수를 저장
11+
result = max(x, result)
12+
for i in a:
13+
# K의 원소로만 구성된 모든 수를 탐색
14+
dfs(x * 10 + i)
15+
16+
dfs(0)
17+
print(result)

Diff for: Exams/solutions/A_3.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import deque
2+
3+
n, m = map(int, input().split())
4+
x, y = map(int, input().split())
5+
6+
# 모든 상대편 말의 위치를 저장
7+
enemy = []
8+
for i in range(m):
9+
a, b = map(int, input().split())
10+
enemy.append((a, b))
11+
12+
# 보드 전체를 -1으로 초기화
13+
board = [[-1] * (n + 1) for _ in range(n + 1)]
14+
# 나이트가 이동할 수 있는 8가지의 위치
15+
dx = [-2, -2, -1, -1, 1, 1, 2, 2]
16+
dy = [-1, 1, -2, 2, -2, 2, -1, 1]
17+
18+
# 현재 위치를 큐에 삽입
19+
q = deque()
20+
q.append((x, y))
21+
# 현재 위치에 대한 최소 이동 수는 0
22+
board[x][y] = 0
23+
24+
# 보드의 모든 위치에 대하여 최소 이동 수를 계산
25+
while q:
26+
x, y = q.popleft()
27+
# 8가지 위치를 각각 확인
28+
for i in range(8):
29+
nx = x + dx[i]
30+
ny = y + dy[i]
31+
# 해당 위치로 이동할 수 있는 경우
32+
if 1 <= nx and nx <= n and 1 <= ny and ny <= n:
33+
# 방문한 적 없다면, 그 위치로의 최소 이동 수 계산
34+
if board[nx][ny] == -1:
35+
board[nx][ny] = board[x][y] + 1
36+
q.append((nx, ny))
37+
38+
# 모든 상대편 말까지의 최소 이동 수 출력
39+
for a, b in enemy:
40+
print(board[a][b], end=' ')

Diff for: Exams/solutions/B_1.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
n, k = map(int, input().split())
2+
data = list(map(int, input().split()))
3+
4+
course = []
5+
for i in range(n):
6+
# (해당 코스의 번호, 해당 코스의 길이)를 입력
7+
course.append((i + 1, data[i]))
8+
# 왕복 코스를 직선 형태로 표현
9+
course.extend(list(reversed(course)))
10+
11+
# 코스의 길이를 앞에서부터 빼내기
12+
i = 0
13+
while True:
14+
k -= course[i][1]
15+
# 지나고 있는 코스 출력 및 종료
16+
if k < 0:
17+
print(course[i][0])
18+
break
19+
i += 1

Diff for: Exams/solutions/B_2.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
n = int(input())
2+
data = list(map(int, input().split()))
3+
# 순서를 바꾸어 '최장 증가 부분 수열' 문제로 변환
4+
data.reverse()
5+
6+
# LCS 알고리즘 수행
7+
dp = [1] * n
8+
for i in range(1, n):
9+
for j in range(0, i):
10+
if data[j] < data[i]:
11+
dp[i] = max(dp[i], dp[j] + 1)
12+
13+
# 열외해야 하는 병사의 최소 수를 출력
14+
print(n - max(dp))

Diff for: Exams/solutions/B_3.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from collections import deque
2+
n, k = map(int, input().split())
3+
4+
# N x N 크기의 보드 전체를 0으로 초기화
5+
board = []
6+
data = []
7+
8+
for i in range(n):
9+
# 보드 정보를 한 줄 단위로 입력
10+
board.append(list(map(int, input().split())))
11+
for j in range(n):
12+
# 해당 위치에 바이러스가 존재하는 경우
13+
if board[i][j] != 0:
14+
# (바이러스 종류, 시간, 위치 X, 위치 Y) 삽입
15+
data.append((board[i][j], 0, i, j))
16+
17+
# 정렬 이후에 큐로 옮기기
18+
data.sort()
19+
q = deque(data)
20+
21+
target_s, target_x, target_y = map(int, input().split())
22+
23+
# 바이러스가 퍼져나갈 수 있는 4가지의 위치
24+
dx = [-1, 0, 1, 0]
25+
dy = [0, 1, 0, -1]
26+
27+
while q:
28+
virus, s, x, y = q.popleft()
29+
# 정확히 s초가 지나거나, 큐가 빌 때까지 반복
30+
if s == target_s:
31+
break
32+
# 4가지 위치를 각각 확인
33+
for i in range(4):
34+
nx = x + dx[i]
35+
ny = y + dy[i]
36+
# 해당 위치로 이동할 수 있는 경우
37+
if 0 <= nx and nx < n and 0 <= ny and ny < n:
38+
# 방문한 적 없다면, 그 위치에 바이러스 넣기
39+
if board[nx][ny] == 0:
40+
board[nx][ny] = virus
41+
q.append((virus, s + 1, nx, ny))
42+
43+
print(board[target_x - 1][target_y - 1])

Diff for: Exams/solutions/C_1.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
n, k = map(int, input().split())
2+
count = 0
3+
4+
# 0시 0분 0초부터 N시 59분 59초까지의 모든 시각 확인
5+
for h in range(n + 1):
6+
for m in range(60):
7+
for s in range(60):
8+
hour = str(h) if h >= 10 else '0' + str(h)
9+
minute = str(m) if m >= 10 else '0' + str(m)
10+
second = str(s) if s >= 10 else '0' + str(s)
11+
# K가 포함되는 경우 카운트
12+
if str(k) in hour + minute + second:
13+
count += 1
14+
15+
print(count)

Diff for: Exams/solutions/C_2.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
n, m, h = map(int, input().split())
2+
a = []
3+
d = [0] * (h + 1)
4+
for i in range(n):
5+
a.append(list(map(int, input().split())))
6+
7+
d[0] = 1
8+
9+
# 모든 학생에 대하여 처리
10+
for i in range(n):
11+
data = []
12+
# 0부터 h까지의 모든 높이에 대하여 처리
13+
for j in range(h + 1):
14+
for k in range(len(a[i])):
15+
# 현재 학생의 블록으로 특정 높이의 탑을 쌓을 수 있는 경우
16+
if d[j] != 0 and j + a[i][k] <= h:
17+
data.append((j + a[i][k], d[j]))
18+
# 쌓을 수 있는 높이에 대하여, 경우의 수 증가
19+
for height, value in data:
20+
d[height] = (d[height] + value) % 10007
21+
22+
print(d[h])

Diff for: Exams/solutions/C_3.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from itertools import combinations
2+
3+
n = int(input())
4+
a = []
5+
teachers = []
6+
spaces = []
7+
for i in range(n):
8+
a.append(list(input().split()))
9+
for j in range(n):
10+
# 선생님이 존재하는 위치 저장
11+
if a[i][j] == 'T':
12+
teachers.append((i, j))
13+
# 장애물을 설치할 수 있는 위치 저장
14+
if a[i][j] == 'X':
15+
spaces.append((i, j))
16+
17+
# 특정 방향으로 감시를 진행 (학생 발견: True, 학생 미발견: False)
18+
def watch(x, y, direction):
19+
# 왼쪽 방향으로 감시
20+
if direction == 0:
21+
while y >= 0:
22+
if a[x][y] == 'S': # 학생이 있는 경우
23+
return True
24+
if a[x][y] == 'O': # 장애물이 있는 경우
25+
return False
26+
y -= 1
27+
# 오른쪽 방향으로 감시
28+
if direction == 1:
29+
while y < n:
30+
if a[x][y] == 'S': # 학생이 있는 경우
31+
return True
32+
if a[x][y] == 'O': # 장애물이 있는 경우
33+
return False
34+
y += 1
35+
# 위쪽 방향으로 감시
36+
if direction == 2:
37+
while x >= 0:
38+
if a[x][y] == 'S': # 학생이 있는 경우
39+
return True
40+
if a[x][y] == 'O': # 장애물이 있는 경우
41+
return False
42+
x -= 1
43+
# 아래쪽 방향으로 감시
44+
if direction == 3:
45+
while x < n:
46+
if a[x][y] == 'S': # 학생이 있는 경우
47+
return True
48+
if a[x][y] == 'O': # 장애물이 있는 경우
49+
return False
50+
x += 1
51+
return False
52+
53+
# 장애물 설치 이후에, 한 명이라도 학생이 감지되는지 검사
54+
def process():
55+
# 모든 선생의 위치를 하나씩 확인
56+
for x, y in teachers:
57+
# 4가지 방향으로 학생을 감지할 수 있는지 확인
58+
for i in range(4):
59+
if watch(x, y, i):
60+
return True
61+
return False
62+
63+
find = False
64+
for data in combinations(spaces, 3):
65+
for x, y in data:
66+
a[x][y] = 'O'
67+
# 학생이 한 명도 감지되지 않는 경우
68+
if not process():
69+
# 원하는 경우를 발견한 것임
70+
find = True
71+
break
72+
for x, y in data:
73+
a[x][y] = 'X'
74+
75+
if find:
76+
print('YES')
77+
else:
78+
print('NO')

Diff for: Exams/solutions/D_1.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
n = input()
2+
length = len(n)
3+
sum = 0
4+
5+
# 왼쪽 부분의 자릿수의 합 더하기
6+
for i in range(length // 2):
7+
sum += int(n[i])
8+
9+
# 오른쪽 부분의 자릿수의 합 빼기
10+
for i in range(length // 2, length):
11+
sum -= int(n[i])
12+
13+
if sum == 0:
14+
print("LUCKY")
15+
else:
16+
print("READY")

Diff for: Exams/solutions/D_2.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from itertools import permutations
2+
3+
n, k = map(int, input().split())
4+
a = list(map(int, input().split()))
5+
result = 0
6+
7+
# 운동 키트를 이용하는 모든 경우를 계산
8+
for data in permutations(a):
9+
check = True
10+
weight = 500
11+
# 매일매일의 운동 키트를 확인
12+
for i in data:
13+
weight += i
14+
weight -= k
15+
# 중량이 500보다 낮아지는 날이 있는지 체크
16+
if weight < 500:
17+
check = False
18+
break
19+
# 매일매일 항상 중량 500 이상이라면, 카운트
20+
if check:
21+
result += 1
22+
23+
print(result)

Diff for: Exams/solutions/D_3.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
n, m = map(int, input().split())
2+
3+
board = []
4+
for _ in range(n):
5+
board.append(list(map(int, input().split())))
6+
v = [[False] * m for _ in range(n)]
7+
8+
def dfs(x, y):
9+
# 아래 끝까지 내려 온 경우 0을 반환
10+
if x == n:
11+
return 0
12+
# 오른쪽 끝까지 이동한 경우 아래로 한 칸 내려가기
13+
if y == m:
14+
return dfs(x + 1, 0)
15+
result = 0
16+
# (x, y)를 기준으로 1번째 모양
17+
if x + 1 < n and y + 1 < m and v[x][y] + v[x][y + 1] + v[x + 1][y + 1] == 0:
18+
v[x][y] = v[x][y + 1] = v[x + 1][y + 1] = True
19+
result = max(result, dfs(x, y + 1) + (board[x][y] + 2 * board[x][y + 1] + board[x + 1][y + 1]))
20+
v[x][y] = v[x][y + 1] = v[x + 1][y + 1] = False
21+
# (x, y)를 기준으로 2번째 모양
22+
if x + 1 < n and y + 1 < m and v[x][y + 1] + v[x + 1][y] + v[x + 1][y + 1] == 0:
23+
v[x][y + 1] = v[x + 1][y] = v[x + 1][y + 1] = True
24+
result = max(result, dfs(x, y + 1) + (board[x][y + 1] + board[x + 1][y] + 2 * board[x + 1][y + 1]))
25+
v[x][y + 1] = v[x + 1][y] = v[x + 1][y + 1] = False
26+
# (x, y)를 기준으로 3번째 모양
27+
if x + 1 < n and y + 1 < m and v[x][y] + v[x + 1][y] + v[x + 1][y + 1] == 0:
28+
v[x][y] = v[x + 1][y] = v[x + 1][y + 1] = True
29+
result = max(result, dfs(x, y + 1) + (board[x][y] + 2 * board[x + 1][y] + board[x + 1][y + 1]))
30+
v[x][y] = v[x + 1][y] = v[x + 1][y + 1] = False
31+
# (x, y)를 기준으로 4번째 모양
32+
if x + 1 < n and y + 1 < m and v[x][y] + v[x][y + 1] + v[x + 1][y] == 0:
33+
v[x][y] = v[x][y + 1] = v[x + 1][y] = True
34+
result = max(result, dfs(x, y + 1) + (2 * board[x][y] + board[x][y + 1] + board[x + 1][y]))
35+
v[x][y] = v[x][y + 1] = v[x + 1][y] = False
36+
# 현재 위치를 기준으로 부메랑을 만들지 않는 경우
37+
result = max(result, dfs(x, y + 1))
38+
return result
39+
40+
print(dfs(0, 0))

Diff for: Exams/solutions/E_1.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 100 x 100 x 100보다 큰 수로, 최적의 해 보장
2+
MAX_VALUE = 10000000
3+
x, y, p1, p2 = map(int, input().split())
4+
5+
# 모든 경우의 수를 포함하는 10,000,000까지 반복
6+
while p1 != p2 and p1 < MAX_VALUE:
7+
if p1 < p2:
8+
p1 += x
9+
else:
10+
p2 += y
11+
12+
# 공통적으로 지나는 지점이 있다면
13+
if p1 < MAX_VALUE:
14+
print(p1)
15+
# 공통적으로 지나는 지점이 없다면
16+
else:
17+
print(-1)

0 commit comments

Comments
 (0)