From 0f0873b4a4f239eaab3d617a13ecac1dce717aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Tue, 3 Sep 2024 18:49:26 +0900 Subject: [PATCH 1/7] #45 11660.py --- aiden/class4-2/11660.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 aiden/class4-2/11660.py diff --git a/aiden/class4-2/11660.py b/aiden/class4-2/11660.py new file mode 100644 index 0000000..872b291 --- /dev/null +++ b/aiden/class4-2/11660.py @@ -0,0 +1,21 @@ +import sys +data = sys.stdin.read().split() + +n, m = int(data[0]), int(data[1]) +arr = [] +index = 2 +for _ in range(n): + arr.append(list(map(int, data[index:index + n]))) + index += n + +dp = [[0] * (n + 1) for _ in range(n + 1)] + +for i in range(1, n + 1): + for j in range(1, n + 1): + dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1] + arr[i-1][j-1] + +for _ in range(m): + x1, y1, x2, y2 = map(int, data[index:index+4]) + result = dp[x2][y2] - dp[x1-1][y2] - dp[x2][y1-1] + dp[x1-1][y1-1] + print(result) + index += 4 From c3fae14163c9d19ffb40a78d077c4751a4f84bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Tue, 3 Sep 2024 18:49:49 +0900 Subject: [PATCH 2/7] #45 9251.py --- aiden/class4-2/9251.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 aiden/class4-2/9251.py diff --git a/aiden/class4-2/9251.py b/aiden/class4-2/9251.py new file mode 100644 index 0000000..de60bcd --- /dev/null +++ b/aiden/class4-2/9251.py @@ -0,0 +1,18 @@ +def lcs(s1, s2): + + dp = [[0] * (len(s2) + 1) for _ in range(len(s1) + 1)] + + for i in range(1, len(s1) + 1): + for j in range(1, len(s2) + 1): + if s1[i-1] == s2[j-1]: + dp[i][j] = dp[i-1][j-1] + 1 + + else: + dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + + return dp[len(s1)][len(s2)] + +s1 = input().strip() +s2 = input().strip() + +print(lcs(s1,s2)) \ No newline at end of file From 6a5b956c23cf3d32fb7eb075cc135887cf773424 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Tue, 3 Sep 2024 19:11:38 +0900 Subject: [PATCH 3/7] #45 12865.py --- aiden/class4-2/12865.py | 48 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 aiden/class4-2/12865.py diff --git a/aiden/class4-2/12865.py b/aiden/class4-2/12865.py new file mode 100644 index 0000000..b932726 --- /dev/null +++ b/aiden/class4-2/12865.py @@ -0,0 +1,48 @@ +# 문제 +# 이 문제는 아주 평범한 배낭에 관한 문제이다. +# +# 한 달 후면 국가의 부름을 받게 되는 준서는 여행을 가려고 한다. +# 세상과의 단절을 슬퍼하며 최대한 즐기기 위한 여행이기 때문에, +# 가지고 다닐 배낭 또한 최대한 가치 있게 싸려고 한다. +# +# 준서가 여행에 필요하다고 생각하는 N개의 물건이 있다. +# 각 물건은 무게 W와 가치 V를 가지는데, 해당 물건을 배낭에 넣어서 가면 준서가 V만큼 즐길 수 있다. +# 아직 행군을 해본 적이 없는 준서는 최대 K만큼의 무게만을 넣을 수 있는 배낭만 들고 다닐 수 있다. +# 준서가 최대한 즐거운 여행을 하기 위해 배낭에 넣을 수 있는 물건들의 가치의 최댓값을 알려주자. + +# 입력 +# 첫 줄에 물품의 수 N(1 ≤ N ≤ 100)과 준서가 버틸 수 있는 무게 K(1 ≤ K ≤ 100,000)가 주어진다. +# 두 번째 줄부터 N개의 줄에 거쳐 각 물건의 무게 W(1 ≤ W ≤ 100,000)와 +# 해당 물건의 가치 V(0 ≤ V ≤ 1,000)가 주어진다. +# +# 입력으로 주어지는 모든 수는 정수이다. +# +# 출력 +# 한 줄에 배낭에 넣을 수 있는 물건들의 가치합의 최댓값을 출력한다. +import sys + +data = sys.stdin.read().split() + +n = int(data[0]) +k = int(data[1]) +index = 2 +cargo = [] + +for i in range(n): + w = int(data[index]) + v = int(data[index + 1]) + cargo.append((w, v)) + index += 2 + +dp = [[0] * (k + 1) for _ in range(n + 1)] + +for i in range(1, n + 1): + weight = cargo[i-1][0] + value = cargo[i-1][1] + for j in range(1, k + 1): + if j < weight: + dp[i][j] = dp[i-1][j] + else: + dp[i][j] = max(dp[i-1][j], dp[i-1][j-weight] + value) + +print(dp[n][k]) From 742134338bc34a2c0e16aef227d456d7d008cc01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Tue, 3 Sep 2024 20:03:07 +0900 Subject: [PATCH 4/7] #45 13549.py --- aiden/class4-2/13549.py | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 aiden/class4-2/13549.py diff --git a/aiden/class4-2/13549.py b/aiden/class4-2/13549.py new file mode 100644 index 0000000..5d23d41 --- /dev/null +++ b/aiden/class4-2/13549.py @@ -0,0 +1,52 @@ +# 문제 +# 수빈이는 동생과 숨바꼭질을 하고 있다. 수빈이는 현재 점 N(0 ≤ N ≤ 100,000)에 있고, +# 동생은 점 K(0 ≤ K ≤ 100,000)에 있다. 수빈이는 걷거나 순간이동을 할 수 있다. +# 만약, 수빈이의 위치가 X일 때 걷는다면 1초 후에 X-1 또는 X+1로 이동하게 된다. +# 순간이동을 하는 경우에는 0초 후에 2*X의 위치로 이동하게 된다. +# +# 수빈이와 동생의 위치가 주어졌을 때, +# 수빈이가 동생을 찾을 수 있는 가장 빠른 시간이 몇 초 후인지 구하는 프로그램을 작성하시오. +# +# 입력 +# 첫 번째 줄에 수빈이가 있는 위치 N과 동생이 있는 위치 K가 주어진다. N과 K는 정수이다. +# +# 출력 +# 수빈이가 동생을 찾는 가장 빠른 시간을 출력한다. +from collections import deque + +def walk(x): + return [x-1,x+1] + +def tel(x): + return x * 2 + +def bfs(n,k): + max_limit = 100001 + visit = [False] * max_limit + distance = [0] * max_limit + + queue = deque([n]) + visit[n] = True + + while queue: + current = queue.popleft() + + if current == k: + return distance[current] + + # 순간이동 처리 (0초 걸림) + next_pos = tel(current) + if 0 <= next_pos < max_limit and not visit[next_pos]: + visit[next_pos] = True + distance[next_pos] = distance[current] + queue.appendleft(next_pos) # 순간이동은 우선순위를 높이기 위해 앞에 추가 + + # 걷기 처리 (1초 걸림) + for next_pos in walk(current): + if 0 <= next_pos < max_limit and not visit[next_pos]: + visit[next_pos] = True + distance[next_pos] = distance[current] + 1 + queue.append(next_pos) + +n, k = map(int, input().split()) +print(bfs(n, k)) From 760a537614da57d425c06ffa1db191a45758eb55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Wed, 4 Sep 2024 01:08:56 +0900 Subject: [PATCH 5/7] #45 1753.py --- aiden/class4-2/1753.py | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 aiden/class4-2/1753.py diff --git a/aiden/class4-2/1753.py b/aiden/class4-2/1753.py new file mode 100644 index 0000000..002785b --- /dev/null +++ b/aiden/class4-2/1753.py @@ -0,0 +1,43 @@ +import sys +import heapq + +data = sys.stdin.read().split() +inf = int(1e9) + +V = int(data[0]) +E = int(data[1]) +K = int(data[2]) + +graph = [[] for _ in range(V + 1)] +distance = [inf] * (V + 1) + +index = 3 + +for _ in range(E): + u = int(data[index]) + v = int(data[index + 1]) + w = int(data[index + 2]) + graph[u].append((v, w)) + index += 3 + +def short_distance(start): + q = [] + heapq.heappush(q, (0, start)) + distance[start] = 0 + while q: + dist, now = heapq.heappop(q) + if distance[now] < dist: + continue + for i in graph[now]: + cost = dist + i[1] + if cost < distance[i[0]]: + distance[i[0]] = cost + heapq.heappush(q, (cost, i[0])) + +short_distance(K) + +for i in range(1, V + 1): + if distance[i] == inf: + print("INF") + else: + print(distance[i]) From c90adad4f4cbedc15cfc04d4484873d3d5854598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Wed, 4 Sep 2024 01:56:49 +0900 Subject: [PATCH 6/7] #45 1967.py --- aiden/class4-2/1967.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 aiden/class4-2/1967.py diff --git a/aiden/class4-2/1967.py b/aiden/class4-2/1967.py new file mode 100644 index 0000000..cffe348 --- /dev/null +++ b/aiden/class4-2/1967.py @@ -0,0 +1,42 @@ +import sys +from collections import deque, defaultdict + +data = sys.stdin.read().split() + +n = int(data[0]) +index = 1 +graph = defaultdict(list) + +for _ in range(n-1): + u, v, w = map(int, data[index:index+3]) + graph[u].append((v, w)) + graph[v].append((u, w)) + index += 3 + +def bfs(start): + distance = [-1] * (n + 1) + distance[start] = 0 + q = deque([start]) + + max_dist = 0 + farthest_node = start + + while q: + node = q.popleft() + + for neighbor, weight in graph[node]: + if distance[neighbor] == -1: + distance[neighbor] = distance[node] + weight + q.append(neighbor) + + if distance[neighbor] > max_dist: + max_dist = distance[neighbor] + farthest_node = neighbor + + return farthest_node, max_dist + +farthest_node, _ = bfs(1) + +_, tree_diameter = bfs(farthest_node) + +print(tree_diameter) From 0eee9fb5479166f7cb56f1382c417a991cffefa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?aiden=2Eji=28=EC=A7=80=EC=9C=A4=EC=88=98=29=20/=20?= =?UTF-8?q?=EC=9D=B8=EA=B3=B5=EC=A7=80=EB=8A=A5?= Date: Wed, 4 Sep 2024 03:20:33 +0900 Subject: [PATCH 7/7] #45 9663.py --- aiden/class4-2/9663.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 aiden/class4-2/9663.py diff --git a/aiden/class4-2/9663.py b/aiden/class4-2/9663.py new file mode 100644 index 0000000..7a7fe4f --- /dev/null +++ b/aiden/class4-2/9663.py @@ -0,0 +1,25 @@ +def safe(row, col): + for i in range(row): + if col == queens[i] or abs(col - queens[i]) == row - i: + return False + return True + +def near_queens(row): + global count # 전역 변수 count를 사용하겠다는 선언 + # 사용 시 def 함수 외에서 선언된 변수를 내부에서 사용 가능 + if row == n: + count += 1 + return + for col in range(n): + if not col_used[col] and not diag1_used[row + col] and not diag2_used[row - col + n - 1]: + col_used[col] = diag1_used[row + col] = diag2_used[row - col + n - 1] = True + near_queens(row + 1) + col_used[col] = diag1_used[row + col] = diag2_used[row - col + n - 1] = False + +n = int(input()) +col_used = [False] * n +diag1_used = [False] * (2 * n - 1) +diag2_used = [False] * (2 * n - 1) +count = 0 +near_queens(0) +print(count)