-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
1,248 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
# 안녕하세요 | ||
# 알고리즘 문제 목록 | ||
|
||
### 다시 풀어봐야하는 문제 | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# https://www.acmicpc.net/problem/4195 | ||
|
||
""" | ||
다른 사람의 아이디어를 참고한 문제 | ||
다른 부분의 로직은 내가 직접 구현한게 맞았지만 사람의 수를 세는 것에서 시간초과가 났다. | ||
처음에는 parent배열에서 같은 부모를 가지는 것들을 조회해서 카운트 했기 때문에 반복문 마다 시행하게 되면 시간복잡도가 O(n^2)이 된다. | ||
해결방법은 새로운 딕셔너리를 이용하는 것이었다.(다른 사람의 아이디어 참고) | ||
union함수를 시행할 때 마다 각각의 집합의 갯수를 더하는 방법을 통해 일일이 조회하지 않고도 사람의 수를 카운트 할 수 있다. | ||
""" | ||
|
||
import sys | ||
n = int(sys.stdin.readline().rstrip()) | ||
|
||
# x는 문자열임 | ||
def find_parent(parent,x): | ||
if parent[x] != x: | ||
parent[x] = find_parent(parent,parent[x]) | ||
return parent[x] | ||
|
||
def union(a,b,parent): | ||
a = find_parent(parent,a) | ||
b = find_parent(parent,b) | ||
|
||
if a > b: | ||
parent[a] = b | ||
cnt[b] += cnt[a] | ||
return b | ||
elif a < b: | ||
parent[b] = a | ||
cnt[a] += cnt[b] | ||
return a | ||
else: | ||
return a | ||
|
||
for i in range(n): | ||
m = int(sys.stdin.readline().rstrip()) | ||
parent = {} | ||
cnt = {} | ||
for j in range(m): | ||
a,b = sys.stdin.readline().rstrip().split() | ||
if a not in parent.keys(): | ||
parent[a] = a | ||
cnt[a] = 1 | ||
if b not in parent.keys(): | ||
parent[b] = b | ||
cnt[b] = 1 | ||
x = union(a,b,parent) | ||
|
||
print(cnt[x]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# https://www.acmicpc.net/problem/11403 | ||
|
||
""" | ||
플로이드 워셜 알고리즘을 응용해서 해결 할 수 있었던 문제 | ||
새로 알아간것은 자기자신에 출발해서 자기자신으로 돌아가는 부분은 거리를 주어질때 음수만 아니면 다른 부분에 영향을 안미친다. | ||
"".join()의 경우 매개변수로 iterable한 객체가 들어가야 되고 ""안에는 구분자를 넣어준다 _를 넣으면 중간에 원소 중간중간에 _를 넣어 반환 | ||
""" | ||
|
||
import sys | ||
from pprint import pprint | ||
|
||
n = int(input()) | ||
graph = [sys.stdin.readline().split() for _ in range(n)] | ||
|
||
for k in range(n): | ||
for i in range(n): | ||
for j in range(n): | ||
if graph[i][k] == '1' and graph[k][j] == '1': | ||
graph[i][j] = '1' | ||
|
||
for i in range(n): | ||
print(" ".join(graph[i])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# https://school.programmers.co.kr/learn/courses/30/lessons/49191 | ||
|
||
# 다른 사람의 아이디어를 보고 해결한 문제 | ||
# 순위 결정되는 조건을 파악하지 못했다. | ||
# 왜 파악하지 못했나? | ||
# 기본적인 조건을 보기보다 어떤 그래프 알고리즘을 적용할지 고민했다. | ||
# 먼저 예시를 도식화하여 당연한 것이라고 생각할지라도 명확하게 정리해야한다. | ||
# 순위가 결정되려면 모든 인원에 관한 승패기록이 존재해야함 | ||
# a가 b를 이기고 b가 c를 이기면 a는 c를 이긴 것이됨 | ||
# 이렇게 직접적으로 결과가 주어지진 않은 것들도 승패 기록을 찾아내려면 플로이드 워셜을 사용하면 됨 | ||
# 이긴것만 기록 하면 되느냐? 아니다 진것도 기록을 해야한다. | ||
|
||
from pprint import pprint | ||
|
||
def solution(n, results): | ||
INF = int(1e9) | ||
graph = [[0] * (n+1) for _ in range(n+1)] | ||
|
||
for a,b in results: | ||
graph[a][b] = 1 | ||
graph[b][a] = -1 | ||
|
||
for i in range(1,n+1): | ||
for j in range(1, n+1): | ||
if i == j: | ||
graph[i][j] = 1 | ||
|
||
pprint(graph) | ||
|
||
for k in range(1,n+1): | ||
for i in range(1,n+1): | ||
for j in range(1,n+1): | ||
if graph[i][k] == 1 and graph[k][j] == 1: | ||
graph[i][j] = 1 | ||
elif graph[i][k] == -1 and graph[k][j] == -1: | ||
graph[i][j] = -1 | ||
|
||
answer = 0 | ||
|
||
pprint(graph) | ||
|
||
for i in range(1,n+1): | ||
if 0 in graph[i][1:]: | ||
continue | ||
answer += 1 | ||
|
||
|
||
return answer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# https://www.acmicpc.net/problem/1976 | ||
|
||
""" | ||
유니온 파인드를 활용해서 해결한 문제 | ||
인접 행렬로는 데이터를 받은 것은 오랜만이라 살짝 헤멨었음 | ||
인접 행렬로 구현하는 것도 가끔 시도해봐야겠다. | ||
""" | ||
|
||
def find_parent(parent,x): | ||
if parent[x] != x: | ||
parent[x] = find_parent(parent,parent[x]) | ||
return parent[x] | ||
|
||
def union(a,b,parent): | ||
a = find_parent(parent,a) | ||
b = find_parent(parent,b) | ||
|
||
if a > b: | ||
parent[a] = b | ||
elif a < b: | ||
parent[b] = a | ||
|
||
|
||
n = int(input()) | ||
m = int(input()) | ||
|
||
parent = [0]*(n+1) | ||
|
||
for i in range(n+1): | ||
parent[i]=i | ||
|
||
graph = [list(map(int,input().split())) for _ in range(n)] | ||
|
||
for i in range(n): | ||
for j in range(n): | ||
if graph[i][j] == 1: | ||
union(i+1,j+1,parent) | ||
|
||
plans = list(map(int,input().split())) | ||
|
||
for i in range(1,n+1): | ||
find_parent(parent,i) | ||
|
||
temp = [] | ||
for city in plans: | ||
temp.append(parent[city]) | ||
|
||
temp = set(temp) | ||
|
||
if len(temp) == 1: | ||
print('YES') | ||
else: | ||
print('NO') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# https://www.acmicpc.net/problem/1717 | ||
|
||
""" | ||
유니온 파인드 기본 개념 문제 | ||
구현할 수 있는지를 물어봄 | ||
""" | ||
|
||
import sys | ||
sys.setrecursionlimit(10**6) | ||
|
||
n,m = map(int,input().split()) | ||
|
||
parent = [0]*(n+1) | ||
|
||
for i in range(n+1): | ||
parent[i]=i | ||
|
||
orders = [list(map(int,sys.stdin.readline().split())) for _ in range(m)] | ||
|
||
def find_parent(parent,x): | ||
if parent[x] != x: | ||
parent[x] = find_parent(parent,parent[x]) | ||
return parent[x] | ||
|
||
def union(a,b,parent): | ||
a = find_parent(parent,a) | ||
b = find_parent(parent,b) | ||
if a > b: | ||
parent[a] = b | ||
elif a < b: | ||
parent[b] = a | ||
|
||
for order in orders: | ||
if order[0] == 0: | ||
union(order[1],order[2],parent) | ||
else: | ||
if find_parent(parent,order[1]) == find_parent(parent,order[2]): | ||
print("yes") | ||
else: | ||
print('no') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# https://www.acmicpc.net/problem/1916 | ||
|
||
# 전형적인 다익스트라 문제 | ||
# 다익스트라를 구현하는데 힙에서 빼온 노드의 거리가 distance리스트에 있는 거리보다 클경우 무시해야한다는 조건을 | ||
# 떠올리지 못해 시간초과가 났었다. | ||
|
||
import heapq | ||
import sys | ||
INF = int(1e9) | ||
|
||
n = int(sys.stdin.readline()) | ||
m = int(sys.stdin.readline()) | ||
graph = [[] for _ in range(n+1)] | ||
|
||
for i in range(m): | ||
a,b,dist = map(int,sys.stdin.readline().split()) | ||
graph[a].append((b,dist)) | ||
|
||
start, end = map(int,sys.stdin.readline().split()) | ||
distance = [INF] * (n+1) | ||
|
||
def dijkstra(start): | ||
|
||
distance[start] = 0 | ||
|
||
heap = [] | ||
heapq.heappush(heap,(0,start)) | ||
|
||
while heap: | ||
dist, city = heapq.heappop(heap) | ||
|
||
if dist > distance[city]: | ||
continue | ||
for node in graph[city]: | ||
new_distance = dist + node[1] | ||
if new_distance < distance[node[0]]: | ||
distance[node[0]] = new_distance | ||
heapq.heappush(heap,(distance[node[0]],node[0])) | ||
|
||
dijkstra(start) | ||
print(distance[end]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# https://www.acmicpc.net/problem/16953 | ||
|
||
""" | ||
bfs적용했지만 메모리 초과가 났던 문제 | ||
방문을 처리하기 위해 목표 숫자의 최댓값 크기의 리스트를 선언했는데 그 크기가 너무 컸기 때문임 | ||
그래서 방문처리를 하기위해 | ||
""" | ||
|
||
from collections import deque,defaultdict | ||
|
||
a,b = map(int,input().split()) | ||
visited = [] | ||
cal_cnt = defaultdict(lambda: -1) | ||
|
||
def bfs(start): | ||
q = deque() | ||
q.append(start) | ||
visited.append(start) | ||
cal_cnt[start] = 1 | ||
|
||
while q: | ||
value = q.popleft() | ||
|
||
num = 2*value | ||
if num <= b and num not in visited: | ||
q.append(num) | ||
cal_cnt[num] = cal_cnt[value] + 1 | ||
|
||
num = 10*value + 1 | ||
if num <= b and num not in visited: | ||
q.append(num) | ||
cal_cnt[num] = cal_cnt[value] + 1 | ||
|
||
bfs(a) | ||
print(cal_cnt[b]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# https://school.programmers.co.kr/learn/courses/30/lessons/159993 | ||
|
||
""" | ||
조건이 존재하는 전형적인 최단 거리 탐색 문제였다. | ||
""" | ||
|
||
from collections import deque | ||
|
||
def solution(maps): | ||
answer = 0 | ||
n = len(maps) | ||
m = len(maps[0]) | ||
|
||
for i in range(n): | ||
for j in range(m): | ||
if maps[i][j] == 'L': | ||
lever_x = i | ||
lever_y = j | ||
elif maps[i][j] == 'E': | ||
exit_x = i | ||
exit_y = j | ||
elif maps[i][j] == 'S': | ||
start_x = i | ||
start_y = j | ||
|
||
distance = bfs(start_x,start_y,n,m,maps) | ||
|
||
if distance[lever_x][lever_y] == -1 or distance[exit_x][exit_y] == -1: | ||
return -1 | ||
|
||
answer += distance[lever_x][lever_y] | ||
|
||
distance = bfs(lever_x,lever_y,n,m,maps) | ||
|
||
answer += distance[exit_x][exit_y] | ||
|
||
|
||
return answer | ||
|
||
def bfs(a,b,n,m,maps): | ||
visited = [[-1]*m for _ in range(n)] | ||
q = deque() | ||
q.append((a,b)) | ||
visited[a][b] = 0 | ||
|
||
dx = [1,0,-1,0] | ||
dy = [0,1,0,-1] | ||
|
||
while q: | ||
x,y = q.popleft() | ||
|
||
for i in range(4): | ||
nx = x + dx[i] | ||
ny = y + dy[i] | ||
if 0<= nx < n and 0 <= ny < m: | ||
if maps[nx][ny] != 'X' and visited[nx][ny] == -1: | ||
q.append((nx,ny)) | ||
visited[nx][ny] = visited[x][y] + 1 | ||
return visited | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.