Skip to content

Commit 81e91cf

Browse files
authored
명환 프로그래머스 숙제(210608) (#86)
1 parent 072bd21 commit 81e91cf

11 files changed

+422
-0
lines changed

‎mhkim/dfsbfs-19.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""
2+
@file dfsbfs-19.py
3+
@brief 연산자 끼워넣기
4+
@desc dfs
5+
"""
6+
7+
INF = int(1e9)+1
8+
9+
n = int(input())
10+
numbers = list(map(int, input().split()))
11+
ops = list(map(int, input().split())) # +,-,*,/
12+
13+
maxVal = -INF
14+
minVal = INF
15+
16+
17+
def dfs(cnt, hap, ops):
18+
global maxVal
19+
global minVal
20+
21+
if cnt == n:
22+
maxVal = max(hap, maxVal)
23+
minVal = min(hap, minVal)
24+
return
25+
26+
if ops[0] > 0:
27+
ops[0] -= 1
28+
dfs(cnt+1, hap+numbers[cnt], ops)
29+
ops[0] += 1
30+
31+
if ops[1] > 0:
32+
ops[1] -= 1
33+
dfs(cnt+1, hap-numbers[cnt], ops)
34+
ops[1] += 1
35+
36+
if ops[2] > 0:
37+
ops[2] -= 1
38+
dfs(cnt+1, hap*numbers[cnt], ops)
39+
ops[2] += 1
40+
41+
if ops[3] > 0:
42+
ops[3] -= 1
43+
if hap < 0:
44+
dfs(cnt+1, -(-hap//numbers[cnt]), ops)
45+
else:
46+
dfs(cnt+1, hap//numbers[cnt], ops)
47+
ops[3] += 1
48+
49+
50+
dfs(1, numbers[0], ops)
51+
52+
print(maxVal)
53+
print(minVal)

‎mhkim/dfsbfs-20.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
@file dfsbfs-20.py
3+
@brief 감시 피하기
4+
@desc
5+
선생과 학생을 제외한 빈 곳 중에서 3곳을 선택하여 벽을 세우고
6+
각 선생이 학생을 감시할 수 있는지 체크 - 네 방향으로 탐색
7+
"""
8+
9+
from itertools import combinations as cb
10+
11+
N = int(input())
12+
graph = [input().split() for _ in range(N)]
13+
14+
dx = [-1, 0, 1, 0]
15+
dy = [0, -1, 0, 1]
16+
17+
# 빈 곳 좌표
18+
empty = []
19+
# 선생님 좌표
20+
teachers = []
21+
22+
for i in range(N):
23+
for j in range(N):
24+
if graph[i][j] == 'X':
25+
empty.append((i, j))
26+
elif graph[i][j] == 'T':
27+
teachers.append((i, j))
28+
29+
30+
def search():
31+
for t in teachers:
32+
for i in range(4):
33+
nx, ny = t
34+
while 1:
35+
nx += dx[i]
36+
ny += dy[i]
37+
38+
if nx < 0 or nx > N-1 or ny < 0 or ny > N-1 or graph[nx][ny] == 'O':
39+
break
40+
41+
if graph[nx][ny] == 'S':
42+
return False
43+
44+
return True
45+
46+
47+
answer = 0
48+
# 벽 3개 뽑은 경우의 수
49+
for walls in cb(empty, 3):
50+
51+
# 벽 만들기
52+
for x, y in walls:
53+
graph[x][y] = 'O'
54+
55+
# 탐색
56+
if search():
57+
answer = 1
58+
break
59+
60+
for x, y in walls:
61+
graph[x][y] = 'X'
62+
63+
if answer == 1:
64+
print('YES')
65+
else:
66+
print('NO')

‎mhkim/dfsbfs-21.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
@file dfsbfs-21.py
3+
@brief 인구 이동
4+
@desc
5+
연합 찾기 - 연결된 나라 (나라마다 체크)
6+
인구 이동 없을 때까지 반복하고 / 연합 국가끼리 인구 분배
7+
8+
deque는 탐색을 위한 / yeonhap은 연합인 나라 저장 공간
9+
"""
10+
11+
import sys
12+
from collections import deque
13+
14+
N, L, R = map(int, sys.stdin.readline().split())
15+
A = [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
16+
17+
dx = [-1, 0, 1, 0]
18+
dy = [0, 1, 0, -1]
19+
20+
21+
def search(x, y, num):
22+
# x, y와 연합인 나라
23+
yeonhap = []
24+
yeonhap.append((x, y))
25+
total = A[x][y] # 연합의 전체 인구 수
26+
union[x][y] = num # 연합 번호
27+
28+
# BFS
29+
q = deque([(x, y)])
30+
while q:
31+
x, y = q.popleft()
32+
for i in range(4):
33+
nx = x + dx[i]
34+
ny = y + dy[i]
35+
if 0 <= nx < N and 0 <= ny < N and union[nx][ny] == -1:
36+
if L <= abs(A[nx][ny] - A[x][y]) <= R:
37+
q.append((nx, ny))
38+
yeonhap.append((nx, ny))
39+
union[nx][ny] = num
40+
total += A[nx][ny]
41+
42+
# 인구 분배
43+
nCountry = len(yeonhap)
44+
for a, b in yeonhap:
45+
A[a][b] = total // nCountry
46+
47+
48+
days = 0
49+
while True:
50+
# 연합 초기화
51+
union = [[-1]*N for _ in range(N)]
52+
num = 0 # 연합국 번호
53+
54+
# 각 나라마다 체크
55+
for i in range(N):
56+
for j in range(N):
57+
if union[i][j] == -1: # 아직 처리되지 않은 나라
58+
search(i, j, num)
59+
num += 1
60+
61+
# 모든 인구 이동
62+
if num == N * N:
63+
break
64+
65+
days += 1
66+
67+
print(days)

‎mhkim/programmers/K번째수.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"""
2+
@file K번째수.py
3+
@brief 정렬 - Level 1
4+
@desc
5+
"""
6+
7+
8+
def solution(array, commands):
9+
answer = []
10+
for i in commands:
11+
arr = sorted(array[i[0]-1:i[1]])
12+
answer.append(arr[i[2]-1])
13+
return answer

‎mhkim/programmers/N으로표현.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"""
2+
@file N으로표현.py
3+
@brief 동적계획법 - Level 3
4+
@desc
5+
1번 사용한 케이스, 2번 사용한 케이스
6+
3번 사용한 케이스 -> 1번과 2번 연산 / 2번과 1번 연산
7+
4번 -> 1번과 3번, 2번과 2번, 3번과 1번 ... 8번까지
8+
9+
구할 수 있는 케이스를 구하고 찾고자 하는 값이 그 케이스들에 존재하는지 확인
10+
"""
11+
12+
# Ver1. 연산 전체
13+
def solution(N, number):
14+
dp = [[]]
15+
16+
x = 0
17+
for i in range(1, 9):
18+
dp.append(set())
19+
x = 10*x + N
20+
dp[i].add(x) # N, NN, NNN...
21+
22+
for j in range(i):
23+
# 연산자 케이스
24+
for op1 in dp[j]:
25+
for op2 in dp[i-j]:
26+
dp[i].add(op1 + op2)
27+
dp[i].add(op1 - op2)
28+
dp[i].add(op1 * op2)
29+
if op2 != 0:
30+
dp[i].add(op1 // op2)
31+
32+
if number in dp[i]:
33+
return i
34+
35+
return -1
36+
37+
# Ver2. 반만 연산
38+
def solution(N, number):
39+
dp = [[]]
40+
41+
x = 0
42+
for i in range(1, 9):
43+
dp.append(set())
44+
x = 10 * x + N
45+
dp[i].add(x) # N, NN, NNN...
46+
47+
for j in range((i//2)+1):
48+
# 연산자 케이스
49+
for op1 in dp[j]:
50+
for op2 in dp[i-j]:
51+
dp[i].add(op1 + op2)
52+
dp[i].add(op1 - op2)
53+
dp[i].add(op2 - op1)
54+
dp[i].add(op1 * op2)
55+
if op1 != 0:
56+
dp[i].add(op2 // op1)
57+
if op2 != 0:
58+
dp[i].add(op1 // op2)
59+
60+
if number in dp[i]:
61+
return i
62+
63+
return -1

‎mhkim/programmers/가장먼노드.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import heapq
2+
3+
INF = int(1e9)
4+
5+
graph = []
6+
distance = []
7+
8+
9+
def dijkstra(start):
10+
global graph
11+
global distance
12+
13+
q = []
14+
heapq.heappush(q, (0, start))
15+
distance[start] = 0
16+
17+
while q:
18+
dist, now = heapq.heappop(q)
19+
if distance[now] < dist:
20+
continue
21+
22+
for i in graph[now]:
23+
cost = dist + i[1]
24+
if cost < distance[i[0]]:
25+
distance[i[0]] = cost
26+
heapq.heappush(q, (cost, i[0]))
27+
28+
29+
def solution(n, edge):
30+
answer = 0
31+
global graph
32+
global distance
33+
34+
graph = [[] for i in range(n+1)]
35+
distance = [INF]*(n+1)
36+
37+
for a, b in edge:
38+
graph[a].append((b, 1))
39+
graph[b].append((a, 1))
40+
41+
dijkstra(1)
42+
43+
maxDist = max(distance[1:])
44+
answer = distance[1:].count(maxDist)
45+
46+
return answer

‎mhkim/programmers/기능개발.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from collections import deque
2+
import math
3+
4+
def solution(progresses, speeds):
5+
answer = []
6+
q = deque()
7+
8+
length = len(progresses)
9+
for i in range(length):
10+
q.append(math.ceil((100-progresses[i])/speeds[i]))
11+
12+
tmp = q.popleft()
13+
cnt = 1
14+
while q:
15+
if tmp < q[0]:
16+
answer.append(cnt)
17+
tmp = q.popleft()
18+
cnt = 1
19+
else:
20+
q.popleft()
21+
cnt += 1
22+
answer.append(cnt)
23+
24+
return answer

‎mhkim/programmers/더맵게.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""
2+
@file 더맵게.py
3+
@brief 힙 - Level 2
4+
@desc
5+
"""
6+
import heapq
7+
8+
9+
def solution(scoville, K):
10+
answer = 0
11+
12+
heapq.heapify(scoville)
13+
while scoville[0] < K:
14+
if len(scoville) < 2:
15+
return -1
16+
17+
first = heapq.heappop(scoville)
18+
second = heapq.heappop(scoville)
19+
heapq.heappush(scoville, first+second*2)
20+
answer += 1
21+
22+
return answer

‎mhkim/programmers/모의고사.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
@file 모의고사.py
3+
@brief 완전탐색 - Level 1
4+
@desc
5+
"""
6+
7+
8+
def solution(answers):
9+
answer = []
10+
11+
supoOne = [1, 2, 3, 4, 5]
12+
supoTwo = [2, 1, 2, 3, 2, 4, 2, 5]
13+
supoThree = [3, 3, 1, 1, 2, 2, 4, 4, 5, 5]
14+
correct = [0, 0, 0]
15+
16+
for i in range(len(answers)):
17+
if answers[i] == supoOne[i % len(supoOne)]:
18+
correct[0] += 1
19+
if answers[i] == supoTwo[i % len(supoTwo)]:
20+
correct[1] += 1
21+
if answers[i] == supoThree[i % len(supoThree)]:
22+
correct[2] += 1
23+
24+
for i in range(len(correct)):
25+
if correct[i] == max(correct):
26+
answer.append(i+1)
27+
28+
answer.sort()
29+
return answer

0 commit comments

Comments
 (0)