diff --git "a/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" "b/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" new file mode 100644 index 0000000..5050135 --- /dev/null +++ "b/larcenous/week_15/BOJ_11779_\354\265\234\354\206\214\353\271\204\354\232\251 \352\265\254\355\225\230\352\270\260 2.py" @@ -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]) \ No newline at end of file diff --git "a/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" "b/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" new file mode 100644 index 0000000..5c21551 --- /dev/null +++ "b/larcenous/week_15/BOJ_12852_1\353\241\234 \353\247\214\353\223\244\352\270\260 2.py" @@ -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) \ No newline at end of file diff --git "a/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" "b/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" new file mode 100644 index 0000000..1de8d82 --- /dev/null +++ "b/larcenous/week_15/BOJ_13913_\354\210\250\353\260\224\352\274\255\354\247\210 4.py" @@ -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)) \ No newline at end of file diff --git "a/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" "b/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" new file mode 100644 index 0000000..1ce8327 --- /dev/null +++ "b/larcenous/week_15/BOJ_14002_\352\260\200\354\236\245 \352\270\264 \354\246\235\352\260\200\355\225\230\353\212\224 \353\266\200\353\266\204 \354\210\230\354\227\264 4.py" @@ -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) \ No newline at end of file diff --git a/larcenous/week_15/BOJ_9019_DSLR.py b/larcenous/week_15/BOJ_9019_DSLR.py new file mode 100644 index 0000000..de86cea --- /dev/null +++ b/larcenous/week_15/BOJ_9019_DSLR.py @@ -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='') \ No newline at end of file diff --git a/larcenous/week_15/BOJ_9252_LCS 2.py b/larcenous/week_15/BOJ_9252_LCS 2.py new file mode 100644 index 0000000..d8c6893 --- /dev/null +++ b/larcenous/week_15/BOJ_9252_LCS 2.py @@ -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) \ No newline at end of file