Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions larcenous/week_15/BOJ_11779_최소비용 구하기 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import sys
from collections import defaultdict
import heapq

INF = sys.maxsize
input = sys.stdin.readline

n = int(input())
m = int(input())
graph = defaultdict(list)
#graph 구성
for _ in range(m):
a, b, c = map(int, input().split())
graph[a].append((b, c))
start, end = map(int, input().split())

dist = [INF] * (n+1)
prev_node = [0] * (n+1) #이전 노드값을 저장하여 역추적에 사용

def dijkstra(start): #start에서 각 노드로 향하는 최단거리 계산
q = []
heapq.heappush(q, (0, start))
dist[start] = 0
while q:
weight, node = heapq.heappop(q)
if dist[node] < weight:
continue
for adj_node, adj_weight in graph[node]:
cost = weight + adj_weight
if cost < dist[adj_node]:
dist[adj_node] = cost
prev_node[adj_node] = node
heapq.heappush(q, (cost, adj_node))

dijkstra(start)
print(dist[end])

#역추적 진행
path = [end]
now = end
while now != start:
now = prev_node[now]
path.append(now)

print(len(path))
print(*path[::-1])
21 changes: 21 additions & 0 deletions larcenous/week_15/BOJ_12852_1로 만들기 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from collections import *
N = int(input())
def BFS(N) :
q = deque([[N]])
cnt = 0
while q :
for _ in range(len(q)) :
path = q.popleft()
x = path[-1]
if x == 1 :
return cnt, path
if x%3 == 0 :
q.append(path + [x//3])
if x%2 == 0 :
q.append(path + [x//2])
q.append(path + [x-1])
cnt += 1

cnt, path = BFS(N)
print(cnt)
print(*path)
32 changes: 32 additions & 0 deletions larcenous/week_15/BOJ_13913_숨바꼭질 4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from collections import deque
N, K = map(int,input().split())
time = [-1]*(100001)
def BFS(N,K) :
q = deque([N])
t = 0
time[N] = (0,N)
if N == K :
return time[N][1], time[N][0]
while q :
for _ in range(len(q)) :
current = q.popleft()
dx=[current+1,current-1,current*2]
for x in dx :
if 0<=x<=100000 and time[x]==-1:
time[x] = (t+1,current)
q.append(x)
if x == K :
return x, t+1
t += 1

def Backtracking(end,t) :
res = [end]
while t != 0 :
end = time[end][-1]
res.append(end)
t -= 1
return res[::-1]

end, t = BFS(N,K)
print(t)
print(*Backtracking(end,t))
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
N = int(input())
A = list(map(int,input().split()))

DP = [1 for _ in range(N)]

for i in range(N):
for j in range(i):
if A[i] > A[j]:
DP[i] = max(DP[i], DP[j]+1)

max_len = max(DP)
print(max_len)
max_idx = DP.index(max_len)
LIS = []

while max_idx >= 0 : #거꾸로 순회
if DP[max_idx] == max_len :
LIS.append(A[max_idx])
max_len -= 1
max_idx -= 1

LIS.reverse()
print(*LIS)
40 changes: 40 additions & 0 deletions larcenous/week_15/BOJ_9019_DSLR.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'''
회전한다는 말에 deque를 쓰면 시간초과가 나기 쉽다
'''
import sys
from collections import deque
input = sys.stdin.readline
T = int(input())
def BFS(A,B) :
visited = [False]*10001
q = deque([(A,'')])
visited[A] = True
while q :
for _ in range(len(q)) :
now = q.popleft()
now_d, now_ins = now[0], now[1]
if now_d == B :
return now_ins
tmp = (2*now_d)%10000
if not visited[tmp] :
visited[tmp] = True
q.append((tmp,now_ins+'D'))

tmp = (now_d-1)%10000
if not visited[tmp] :
visited[tmp] = True
q.append((tmp,now_ins+'S'))

tmp = now_d // 1000 + (now_d%1000)*10
if not visited[tmp] :
visited[tmp] = True
q.append((tmp,now_ins+'L'))

tmp = now_d // 10 + (now_d%10)*1000
if not visited[tmp] :
visited[tmp] = True
q.append((tmp,now_ins+'R'))

for _ in range(T) :
A,B = map(int,input().split())
print(*BFS(A,B),sep='')
35 changes: 35 additions & 0 deletions larcenous/week_15/BOJ_9252_LCS 2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
A = input().rstrip()
B = input().rstrip()

def LCS_DP(A,B) :
DP = [[0]*(len(B)+1) for _ in range(len(A)+1)]
for i in range(1,len(A)+1):
for j in range(1,len(B)+1):
if A[i-1] != B[j-1] :
DP[i][j]=max(DP[i][j-1],DP[i-1][j])
else :
DP[i][j]=DP[i-1][j-1]+1
return DP

#print(LCS_DP(A,B))

def LCS(DP) : #역추적을 하는 함수
result = []
i, j = len(A),len(B) #DP 2차원 배열의 가로 세로 끝 index 지정
maximum = DP[-1][-1]
print(maximum)
if maximum > 0 :
while maximum != 0 and i > 0 and j > 0:
if maximum == DP[i-1][j] :
i-=1
elif maximum == DP[i][j-1] :
j-=1
else :
result.append(A[i-1])
maximum -= 1
i-=1
j-=1
print(*result[::-1], sep="")

tmp = LCS_DP(A,B)
LCS(tmp)