From 9095fab7ec1d136c31b12460919ed947fc2f88f7 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Mon, 17 May 2021 15:31:45 +0900 Subject: [PATCH 1/5] 2469 --- syheo/codingTest1/Baekjoon/2469.py | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 syheo/codingTest1/Baekjoon/2469.py diff --git a/syheo/codingTest1/Baekjoon/2469.py b/syheo/codingTest1/Baekjoon/2469.py new file mode 100644 index 0000000..31b55fc --- /dev/null +++ b/syheo/codingTest1/Baekjoon/2469.py @@ -0,0 +1,65 @@ +#solved.ac +#실버1 +#? +#사다리 타기 +#2469 +# 10 +# 5 +# 첫번쨰 아이디어 +# ????줄의 경우의 수를 bfs로 모두 구한 뒤 입력받은 결과와 비교 -> 시간초과 +# 두번쨰 아이디어 +# ????줄 기준 위 아래 결과를 구하고(first_sort,second_sort), ????줄에 의해 둘이 같아질 수 있는지 검사. + +import sys +input=sys.stdin.readline + +#??????줄이 나오기 전까지 sort, 나오면 해당 줄 리턴 +def first_sort(n,k): + for i in range(n): + for j in range(k-1): + if ladders[i][j]=='?': + return i + elif ladders[i][j]=='-': + first_persons[j],first_persons[j+1]=first_persons[j+1],first_persons[j] + +def second_sort(n,questionIdx): + for i in range(n-1,questionIdx,-1): + for j in range(k-1): + if ladders[i][j]=='-': + persons[j],persons[j+1]=persons[j+1],persons[j] + + +#사람 수 , 가로줄 수, 사다리 결과 +k = int(input()) +n = int(input()) +persons = list(input()) +#A~?(k 만큼 알파벳 솔팅) +first_persons = [chr(65+i) for i in range(k)] + +ladders = [] + +for i in range(n): + ladders.append(list(input())) + +questionIdx = first_sort(n,k) +second_sort(n,questionIdx) + +isNoCase = False +res = '' +#정답 가능 줄 만들기 +for i in range(k-1): + if first_persons[i]==persons[i]: + res += '*' + elif first_persons[i] == persons[i+1]: + res += '-' + elif i !=0 and first_persons[i] == persons[i-1] and res[i-1]=='-': + res += '*' + else: + isNoCase=True + break + +if isNoCase: + for i in range(k-1): + print('x',end='') +else: + print(res) From 51b047f831db5b7531830a4cc2b727298b1b62d0 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 18 May 2021 14:45:07 +0900 Subject: [PATCH 2/5] 1939 --- a | 0 b | 0 syheo/codingTest1/Baekjoon/1939.py | 72 ++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 a create mode 100644 b create mode 100644 syheo/codingTest1/Baekjoon/1939.py diff --git a/a b/a new file mode 100644 index 0000000..e69de29 diff --git a/b b/b new file mode 100644 index 0000000..e69de29 diff --git a/syheo/codingTest1/Baekjoon/1939.py b/syheo/codingTest1/Baekjoon/1939.py new file mode 100644 index 0000000..81988c2 --- /dev/null +++ b/syheo/codingTest1/Baekjoon/1939.py @@ -0,0 +1,72 @@ +#solved.ac +#골드4 +#? +#중량제한 +#1939 + +# 첫번쨰 아이디어 +# 인접 행렬로 bfs만 돌음 -> 메모리초과 +# 인접 리스트로 bfs만 돌음 -> 메모리 초과 +# 중량 초과하면 간선 가중치만큼 길을 통과할 수 있다고 생각함. +# 중량 초과하면 아예 가질 못함 +# 두번째 아이디어 +# 중량을 이진탐색으로 선택, bfs 돌려서 갈 수 있는 경우와 못가는 경우로 left, right 조절 + +from collections import deque + +INF = int(1e9) + +def binary_search(A,B): + answer = 0 + left = 0 + right = INF + mid = (left+right)//2 + while left<=right: + #방문 배열 초기화 + visited = [False for _ in range(N+1)] + if bfs(A,B,mid,visited): + answer = max(mid,answer) + left = mid+1 + else: + right = mid-1 + mid = (left+right)//2 + return answer + +def bfs(A,B,mid,visited): + q = deque([A]) # 섬, 비용 + visited[A]=True + while q: + land = q.popleft() + for info in graphs[land]: + dest = info[0] + destCost = info[1] + #문제를 잘못 이해함 + #c = cost if destCost>=cost else destCost + #중량제한 통과 + if mid<=destCost: + #방문했던 노드가 아니면 + if not visited[dest]: + visited[dest]=True + if dest==B: + return True + else: + q.append(dest) + + return False + +#섬 개수, 도로 갯수 +N, M = map(int,input().split()) +#인접 리스트 초기화 +graphs = [[] for _ in range(N+1)] +#인접 리스트 입력 +for i in range(M): + a,b,c = map(int,input().split()) + #a->b;c , b->a;c + graphs[a].append((b,c)) + graphs[b].append((a,c)) + +# 공장이 있는 두 개의 섬 +A,B = map(int,input().split()) + +# binarySearch and bfs +print(binary_search(A,B)) From a913045a71c84c1256aef114186a894bc609d907 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 18 May 2021 14:45:34 +0900 Subject: [PATCH 3/5] delete file --- a | 0 b | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 a delete mode 100644 b diff --git a/a b/a deleted file mode 100644 index e69de29..0000000 diff --git a/b b/b deleted file mode 100644 index e69de29..0000000 From 44c5f126aa1e4df8ce5b3ce13cd648251870e0b5 Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Tue, 18 May 2021 15:50:24 +0900 Subject: [PATCH 4/5] 5972 --- syheo/codingTest1/Baekjoon/5972-2.py | 40 ++++++++++++++++++++++++++++ syheo/codingTest1/Baekjoon/5972.py | 40 ++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 syheo/codingTest1/Baekjoon/5972-2.py create mode 100644 syheo/codingTest1/Baekjoon/5972.py diff --git a/syheo/codingTest1/Baekjoon/5972-2.py b/syheo/codingTest1/Baekjoon/5972-2.py new file mode 100644 index 0000000..e3f87e0 --- /dev/null +++ b/syheo/codingTest1/Baekjoon/5972-2.py @@ -0,0 +1,40 @@ +#solved.ac +#골드5 +#? +#택배 배송 +#5972 + +#다익스트라 + +import heapq + +def dijkstra(N,dp): + q = [] + heapq.heappush(q,(1,0)) #현재 위치, 여물 수 + dp[1]=0 + while q: + node, cost = heapq.heappop(q) + if dp[node]cost+c: + dp[dest]=cost+c + heapq.heappush(q,(dest,cost+c)) + + +N,M = map(int,input().split()) +INF = int(1e9) +graphs = [[] for _ in range(N+1)] +for i in range(M): + a,b,c = map(int,input().split()) + graphs[a].append((b,c)) + graphs[b].append((a,c)) + +dp = [INF for _ in range(N+1)] + +dijkstra(N,dp) + +print(dp[N]) + diff --git a/syheo/codingTest1/Baekjoon/5972.py b/syheo/codingTest1/Baekjoon/5972.py new file mode 100644 index 0000000..9ca6c7a --- /dev/null +++ b/syheo/codingTest1/Baekjoon/5972.py @@ -0,0 +1,40 @@ +#solved.ac +#골드5 +#? +#택배 배송 +#5972 + +#bfs+다익스트라 일부 + +from collections import deque + +def bfs(N,dp): + q = deque([(1,0)]) #현재 위치, 여물 수 + dp[1]=0 + while q: + print(q) + node, cost = q.popleft() + if dp[node]cost+c: + dp[dest]=cost+c + q.append((dest,cost+c)) + + +N,M = map(int,input().split()) +INF = int(1e9) +graphs = [[] for _ in range(N+1)] +for i in range(M): + a,b,c = map(int,input().split()) + graphs[a].append((b,c)) + graphs[b].append((a,c)) + +dp = [INF for _ in range(N+1)] + +bfs(N,dp) + +print(dp[N]) + From 8f218d44cb8cabf219d50721041b76c2b2886c2f Mon Sep 17 00:00:00 2001 From: heoseungyeon Date: Wed, 19 May 2021 15:21:36 +0900 Subject: [PATCH 5/5] 5972 fix --- syheo/codingTest1/Baekjoon/5972-2.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/syheo/codingTest1/Baekjoon/5972-2.py b/syheo/codingTest1/Baekjoon/5972-2.py index e3f87e0..451d831 100644 --- a/syheo/codingTest1/Baekjoon/5972-2.py +++ b/syheo/codingTest1/Baekjoon/5972-2.py @@ -5,15 +5,16 @@ #5972 #다익스트라 - +import sys import heapq +input = sys.stdin.readline -def dijkstra(N,dp): +def bfs(N,dp): q = [] - heapq.heappush(q,(1,0)) #현재 위치, 여물 수 + heapq.heappush(q,(0,1)) #여물 수, 현재 위치 dp[1]=0 while q: - node, cost = heapq.heappop(q) + cost, node = heapq.heappop(q) if dp[node]cost+c: dp[dest]=cost+c - heapq.heappush(q,(dest,cost+c)) + heapq.heappush(q,(cost+c,dest)) N,M = map(int,input().split()) @@ -34,7 +35,6 @@ def dijkstra(N,dp): dp = [INF for _ in range(N+1)] -dijkstra(N,dp) - -print(dp[N]) +bfs(N,dp) +print(dp[N]) \ No newline at end of file