Skip to content

Commit 8b6f83f

Browse files
authored
Merge pull request #155 from zaqquum/main
Hongjoo/3월 3주차 / 6개
2 parents 1838c5d + 27f5b41 commit 8b6f83f

File tree

12 files changed

+452
-0
lines changed

12 files changed

+452
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from collections import deque
2+
def solution(maps):
3+
n,m = len(maps) ,len(maps[0])# y길이 # x 길이
4+
5+
# 이동 범위 - 하,우,상,좌
6+
dx = [0,1 , 0,-1]
7+
dy = [1 , 0 , -1 , 0]
8+
queue = deque([(0,0)])
9+
visited = [[False for _ in range(m)] for k in range(n)]
10+
visited[0][0] = True
11+
#2. BFS
12+
while queue :
13+
# 현위치
14+
curr_y , curr_x = queue.popleft()
15+
16+
# 통로 확인 - 벽or 바운더리 밖이면 x
17+
for i in range(4):
18+
next_y = curr_y + dy[i]
19+
next_x = curr_x + dx[i]
20+
# target 도착 확인
21+
if next_y == n-1 and next_x == m-1 and maps[next_y][next_x] == 1:
22+
visited[next_y][next_x] = True
23+
maps[next_y][next_x] = maps[curr_y][curr_x] +1
24+
break
25+
# 바운더리 안에 있음 * 길인 경우 + 방문 등록 x
26+
if 0<=next_y< n and 0<= next_x < m :
27+
if maps[next_y][next_x] == 1 and not visited[next_y][next_x]:
28+
maps[next_y][next_x] = maps[curr_y][curr_x] +1
29+
queue.append((next_y, next_x))
30+
visited[next_y][next_x] = True
31+
32+
33+
# 3. 최단 거리 출력
34+
# print(answer_list)
35+
if not visited[n-1][m-1]:
36+
return -1
37+
# answer = min(answer_list)
38+
answer= maps[-1][-1]
39+
# print(answer)
40+
return answer

Hongjoo/lv2/완전범죄 .py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def solution(info, n, m):
2+
answer = 0
3+
#1. dp 테이블 정의
4+
dp = [[] for _ in range(len(info))]
5+
6+
if info[0][0] < n:
7+
dp[0].append((info[0][0] , 0))
8+
if info[0][1] < m :
9+
dp[0].append((0, info[0][1]))
10+
if len(dp[0]) <= 0 :
11+
return -1
12+
# print(f" # item 0 : {dp[0]}")
13+
for i in range(1, len(info)):
14+
# dp[i][k]
15+
for cur_a ,cur_b in dp[i-1]:
16+
# print(f"##cur_a ,cur_b {cur_a},{cur_b }")
17+
up_a = cur_a + info[i][0]
18+
up_b = cur_b + info[i][1]
19+
if up_a < n:
20+
dp[i].append((up_a , cur_b))
21+
if up_b < m :
22+
dp[i].append((cur_a , up_b))
23+
# print(f" # item {i} : {dp[i]}")
24+
if len(dp[i]) <= 0 :
25+
return -1
26+
# print(f" # item {i} : {dp[i]}")
27+
# A 최소값 반환
28+
answer = sorted(dp[-1])[0][0]
29+
return answer

Hongjoo/lv2/완전범죄.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
def solution(info, n, m):
2+
answer = 0
3+
#1. dp 테이블 정의
4+
dp = [[] for _ in range(len(info))]
5+
6+
if info[0][0] < n:
7+
dp[0].append((info[0][0] , 0))
8+
if info[0][1] < m :
9+
dp[0].append((0, info[0][1]))
10+
if len(dp[0]) <= 0 :
11+
return -1
12+
# print(f" # item 0 : {dp[0]}")
13+
for i in range(1, len(info)):
14+
# dp[i][k]
15+
for cur_a ,cur_b in dp[i-1]:
16+
# print(f"##cur_a ,cur_b {cur_a},{cur_b }")
17+
up_a = cur_a + info[i][0]
18+
up_b = cur_b + info[i][1]
19+
if up_a < n:
20+
dp[i].append((up_a , cur_b))
21+
if up_b < m :
22+
dp[i].append((cur_a , up_b))
23+
# print(f" # item {i} : {dp[i]}")
24+
if len(dp[i]) <= 0 :
25+
return -1
26+
# print(f" # item {i} : {dp[i]}")
27+
# A 최소값 반환
28+
answer = sorted(dp[-1])[0][0]
29+
return answer

Hongjoo/lv3/단어변환.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# 최소 편집 거리 =1 확인
2+
def check_edit1(a,b):
3+
diff= 0
4+
for i in range(len(a)) :
5+
if a[i] != b[i] :
6+
diff+=1
7+
if diff > 1 :
8+
return False
9+
if diff == 1 :
10+
return True
11+
12+
from collections import deque
13+
def solution(begin, target, words):
14+
answer = 0
15+
#excep1 :words 에 target없으면 -> 변환 불가
16+
if target not in words :
17+
return 0
18+
19+
# 1. 인접 graph 생성
20+
graph = [[] for _ in range(len(words)+2)]
21+
# begin -> words, target 단방향
22+
for k in range(len(words)) :
23+
if check_edit1(begin, words[k]) :
24+
graph[0].append(k+1)
25+
if check_edit1(begin,target):
26+
graph[0].append(len(words)+1)
27+
# words -> wrods_1(쌍방향) , target(단방향)
28+
for k in range(len(words)):
29+
for j in range(k ,len(words)):
30+
if check_edit1(words[k], words[j]):
31+
graph[k+1].append(j+1)
32+
graph[j+1].append(k+1)
33+
if check_edit1(words[k], target):
34+
graph[k+1].append(len(words)+1)
35+
36+
#2.BFS
37+
visited = [ 0 for _ in range(len(words)+2)]
38+
def bfs(visited):
39+
visited[0] = 0
40+
queue = deque()
41+
queue.append(0)
42+
43+
while queue :
44+
curr_idx = queue.popleft()
45+
if len(graph[curr_idx]) <= 0 : # 이웃한 놈이 없는 경우 => return 0
46+
return 0
47+
48+
for next_idx in graph[curr_idx] :
49+
if not visited[next_idx] :
50+
51+
queue.append(next_idx)
52+
visited[next_idx] = visited[curr_idx] + 1
53+
54+
if visited[-1] : #성공
55+
return visited[-1]
56+
57+
return visited[-1]
58+
59+
60+
# print(graph)

Hongjoo/lv3/아이템줍기.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"""
2+
2hour
3+
- 직사각형 바깥 테두리 이동
4+
- x축, y축 좌표가 같은 경우 없음(꼭짓점, 변 공유 x )
5+
- 분리x, 포함x => only 겹침
6+
goal) 캐릭터(start) -> 아이템 최단거리(BFS)
7+
8+
#key idea : 좌표 & grid 2배 처리하기
9+
(참고.https://jyeonnyang2.tistory.com/247#google_vignette)
10+
#1. 인접 행렬
11+
-field[x][y] = 길에 해당하는 (x,y) 칸 = 1 ,길 x 인 칸은 (x,y )= 0
12+
13+
-(1) 각 직사각형 테두리에 해당하는 좌표 선출
14+
- (2) A- > B 각 범위에 겹치는 테두리 자표 제거[필터링]
15+
-> 내부 빈 공간은 상관x
16+
#2. BFS
17+
- visited 사용 to 누적 거리
18+
"""
19+
from collections import deque
20+
def get_outlier(x1,y1,x2,y2):
21+
22+
pos_outliers = list()
23+
# 가로
24+
for x in range(x1,x2+1,1):
25+
pos_outliers.append([x,y1])
26+
pos_outliers.append([x,y2])
27+
# 세로
28+
for y in range(y1, y2+1, 1):
29+
if [x1,y] in pos_outliers or [x2,y] in pos_outliers:
30+
continue
31+
pos_outliers.append([x1,y])
32+
pos_outliers.append([x2,y])
33+
return sorted(pos_outliers)
34+
35+
36+
def solution(rectangle, characterX, characterY, itemX, itemY):
37+
answer = 0
38+
field = [[0 for _ in range(51*2)] for k in range(51*2)]
39+
#인접 행렬
40+
characterX, characterY = characterX*2 , characterY*2
41+
itemX, itemY = itemX*2 , itemY*2
42+
#1.각 직사각형 칸 획득
43+
for k in range(len(rectangle)):
44+
#outlier 칸 찾기
45+
x1,y1 ,x2 ,y2 = rectangle[k]
46+
x1 = x1*2 ; x2 = x2*2 ; y1 = y1*2 ; y2 = y2*2 # 2배
47+
outliers = get_outlier(x1,y1,x2,y2)
48+
49+
#필터링
50+
# 모든 변 = 1 - 영역 겹치는 모서리만 =0
51+
for i in range(len(outliers)):
52+
out_x , out_y= outliers[i]
53+
field[out_x][out_y] = 1
54+
# 영역 겹치는 모서리 탐색 -> =0
55+
for j in range(len(rectangle)) :
56+
if j == k : # 본인껀 스킵
57+
continue
58+
fil_x1 , fil_y1 , fil_x2 , fil_y2 = rectangle[j]
59+
fil_x1 , fil_y1 =fil_x1*2 , fil_y1*2
60+
fil_x2 , fil_y2 = fil_x2*2 , fil_y2*2
61+
for i in range(len(outliers)):
62+
out_x , out_y = outliers[i]
63+
# 면적이 겹침 -> 필터링
64+
if fil_x2 > out_x > fil_x1 and fil_y2 > out_y > fil_y1:
65+
field[out_x][out_y] = 0
66+
67+
# for i in range(51):
68+
# for k in range(51):
69+
# if field[i][k] == 1:
70+
# print (f"[{i},{k}")
71+
72+
# stage 2. BFS로 최단 거리 찾기
73+
# 상하좌우
74+
dx =[0,0,1,-1]
75+
dy= [1,-1,0,0]
76+
queue = deque([(characterX,characterY)]) #queue 초기화
77+
field[characterX][characterY]=0
78+
while queue :
79+
cur_x, cur_y = queue.popleft()
80+
#인접 노드
81+
for idx in range(4):
82+
# 루트만 가능
83+
next_x ,next_y = cur_x + dx[idx] , cur_y + dy[idx]
84+
if field[next_x][next_y] == 1 : # 방문 x & 길
85+
field[next_x][next_y] = field[cur_x][cur_y] +1
86+
# print(f"#{field[next_x][next_y]}")
87+
if next_x == itemX and next_y == itemY: # 목적지 도착 -> 결과 반환
88+
# print(f"#####{field[next_x][next_y]}")
89+
return field[next_x][next_y]//2
90+
break
91+
queue.append((next_x,next_y))
92+
93+
# print(f"#: {cur_x},{cur_y} => {field[cur_x][cur_y]}")
94+
95+
# print(f">{queue}")
96+
# print(f"##{field[next_x][next_y]}")
97+
98+
99+
return answer

Hongjoo/백준/데스노트.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
https://www.acmicpc.net/problem/2281
3+
# 참고 : https://cme10575.tistory.com/161
4+
"""
5+
import sys
6+
input = sys.stdin.readline
7+
8+
n, m = map(int, input().split())
9+
10+
names = [int(input()) for _ in range(n)]
11+
dp = [[-1]*(m+1) for _ in range(n)]
12+
dp[0][names[0]] = 0
13+
14+
for r in range(n-1):
15+
for c in range(1, m+1):
16+
if dp[r][c] != -1:
17+
#뒤에 붙이는 경우의 수
18+
if c+1+names[r+1] <= m:
19+
dp[r+1][c+names[r+1]+1] = dp[r][c]
20+
#다음줄에 쓰는 경우의 수
21+
if dp[r+1][names[r+1]] != -1:
22+
dp[r+1][names[r+1]] = min(dp[r+1][names[r+1]], dp[r][c] + (m-c)**2)
23+
else:
24+
dp[r+1][names[r+1]] = dp[r][c] + (m-c)**2
25+
26+
answer = 1000000000
27+
for i in range(1, m+1):
28+
if dp[n-1][i] != -1:
29+
answer = min(answer, dp[n-1][i])
30+
print(answer)

Hongjoo/백준/동전1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
https://www.acmicpc.net/problem/2293
3+
4+
# 문제
5+
- 가치가 다른 n 개의 종류의 동전을 활용해 총합 k 가 되는 경우의 수
6+
- 사용 순서는 구분하지 않는다(중복제외)
7+
8+
# 경우의 수
9+
# brude force
10+
- n <= 100
11+
12+
1. 목표 금액 k에 도달하기 까지 divde conquer /사용하는 동전 종류도 사전 순서대로 사용
13+
dp[lv][k] = dp[lv-1][k] + dp[lv][k-c]
14+
2. 메모리 초과
15+
=> dp 는 1차원 테이블로
16+
17+
"""
18+
import sys
19+
20+
n , k = map(int,sys.stdin.readline().split())
21+
coins = list()
22+
dp = [0 for _ in range(k+1)]
23+
for _ in range(n):
24+
coins.append(int(sys.stdin.readline()))
25+
# 가치가 작은 coin 부터 적용
26+
coins.sort()
27+
dp[0] = 1
28+
29+
for c in coins:
30+
for i in range(c, k+1) :
31+
dp[i] = dp[i] + dp[i-c]
32+
print(dp[k])

Hongjoo/백준/동전2.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
https://www.acmicpc.net/problem/2294
3+
# dp
4+
- n개의 동전 조합으로 가치 합이 k 가 되는 경우
5+
- 그 중 사용 동전 최소
6+
- 메모리 제한 128MB
7+
# 문제
8+
-dp[0] = 0 ,
9+
j : 사용 가능한 coin 종류
10+
DP[k] = min(dp이전[k] , "dp[k-c]+1 )
11+
"""
12+
import sys
13+
14+
n , k = map(int, sys.stdin.readline().split())
15+
coins = list()
16+
for _ in range(n):
17+
coins.append(int(sys.stdin.readline()))
18+
19+
coins.sort()
20+
21+
# 2. dp 테이블 초기화
22+
INF= int(1e9)
23+
dp = [INF for _ in range(k+1)]
24+
dp[0] = 0
25+
for c in coins :
26+
for j in range(c, k+1) :
27+
dp[j] = min(dp[j] , dp[j-c]+1)
28+
29+
#3. 출력 - 업데이트x 면 -1 출력
30+
if dp[k] == INF :
31+
print(-1)
32+
else :
33+
print(dp[k])

0 commit comments

Comments
 (0)