From 8d564b5dae604786b9e0847d03eb32e77191b290 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Thu, 2 Oct 2025 12:34:22 +0900 Subject: [PATCH 1/6] =?UTF-8?q?[donghyeon95]=20beakjoon=2019236.=20?= =?UTF-8?q?=EC=B2=AD=EC=86=8C=EB=85=84=20=EC=83=81=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../donghyeon95.java" | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 "BaekJoon/19236/ \354\262\255\354\206\214\353\205\204 \354\203\201\354\226\264/donghyeon95.java" diff --git "a/BaekJoon/19236/ \354\262\255\354\206\214\353\205\204 \354\203\201\354\226\264/donghyeon95.java" "b/BaekJoon/19236/ \354\262\255\354\206\214\353\205\204 \354\203\201\354\226\264/donghyeon95.java" new file mode 100644 index 0000000..bdb7be2 --- /dev/null +++ "b/BaekJoon/19236/ \354\262\255\354\206\214\353\205\204 \354\203\201\354\226\264/donghyeon95.java" @@ -0,0 +1,106 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.StringTokenizer; + +public class Main { + // 1~8 방향 (상, 좌상, 좌, 좌하, 하, 우하, 우, 우상) + static int[][] moves = { + {}, + {0, -1}, {-1, -1}, {-1, 0}, {-1, 1}, + {0, 1}, {1, 1}, {1, 0}, {1, -1} + }; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int[][] fishValue = new int[4][4]; // 물고기 번호 + int[][] fishMove = new int[4][4]; // 물고기 방향 + + for (int j = 0; j < 4; j++) { + StringTokenizer st = new StringTokenizer(br.readLine()); + for (int i = 0; i < 4; i++) { + int a = Integer.parseInt(st.nextToken()); // 물고기 번호 + int b = Integer.parseInt(st.nextToken()); // 방향 + fishValue[j][i] = a; + fishMove[j][i] = b; + } + } + + System.out.println(dfs(fishValue, fishMove, 0, 0, 0)); + } + + public static int dfs(int[][] fishValue, int[][] fishMove, int sharkX, int sharkY, int sharkMove) { + // 벽을 넘거나 물고기가 없을 때는 종료 + if (sharkX < 0 || sharkX > 3 || sharkY < 0 || sharkY > 3 || fishValue[sharkY][sharkX] == 0) { + return 0; + } + + // 상어가 먹는다 + int answer = fishValue[sharkY][sharkX]; + int newSharkMove = fishMove[sharkY][sharkX]; + fishValue[sharkY][sharkX] = 0; + fishMove[sharkY][sharkX] = 0; + + // 물고기 이동 + for (int i = 1; i <= 16; i++) { + int[] fish = findFish(fishValue, i); + if (fish == null) continue; + + int move = fishMove[fish[1]][fish[0]]; + int newFishX = fish[0] + moves[move][0]; + int newFishY = fish[1] + moves[move][1]; + + // 회전해서 갈 수 있는 자리 찾기 + for (int j = 0; j < 8; j++) { + if (newFishX < 0 || newFishX > 3 || newFishY < 0 || newFishY > 3 || (newFishX == sharkX && newFishY == sharkY)) { + move = (move == 8) ? 1 : move + 1; // 방향 회전 + newFishX = fish[0] + moves[move][0]; + newFishY = fish[1] + moves[move][1]; + } else break; + } + + // 물고기 스왑 + int fishNum = fishValue[fish[1]][fish[0]]; + int tempValue = fishValue[newFishY][newFishX]; + int tempMove = fishMove[newFishY][newFishX]; + + fishValue[newFishY][newFishX] = fishNum; + fishMove[newFishY][newFishX] = move; + fishValue[fish[1]][fish[0]] = tempValue; + fishMove[fish[1]][fish[0]] = tempMove; + } + + // 상어 이동 (최대 3칸) + int result = 0; + for (int i = 1; i <= 3; i++) { + int newSharkX = sharkX + (moves[newSharkMove][0] * i); + int newSharkY = sharkY + (moves[newSharkMove][1] * i); + + result = Math.max(result, + dfs(cloneArr(fishValue), cloneArr(fishMove), newSharkX, newSharkY, newSharkMove)); + } + + return answer + result; + } + + // 배열 깊은 복사 + public static int[][] cloneArr(int[][] arr) { + int[][] newArr = new int[4][4]; + for (int i = 0; i < 4; i++) { + newArr[i] = arr[i].clone(); + } + return newArr; + } + + // 특정 번호의 물고기 찾기 + public static int[] findFish(int[][] fishValue, int fishNum) { + for (int i = 0; i < 4; i++) { + for (int j = 0; j < 4; j++) { + if (fishValue[i][j] == fishNum) { + return new int[]{j, i}; + } + } + } + return null; + } +} From d7c857a0c790f3af3c3ab686b107bbc612a342e2 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Thu, 2 Oct 2025 17:04:16 +0900 Subject: [PATCH 2/6] [donghyeon95] beakjoon 9251. LCS --- BaekJoon/9251/ LCS/donghyeon95.java | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 BaekJoon/9251/ LCS/donghyeon95.java diff --git a/BaekJoon/9251/ LCS/donghyeon95.java b/BaekJoon/9251/ LCS/donghyeon95.java new file mode 100644 index 0000000..2a779e0 --- /dev/null +++ b/BaekJoon/9251/ LCS/donghyeon95.java @@ -0,0 +1,40 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class Main { + static int[][] dp; + static String s1, s2; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + s1 = br.readLine(); + s2 = br.readLine(); + + dp = new int[s1.length()][s2.length()]; + for (int i = 0; i < s1.length(); i++) { + Arrays.fill(dp[i], -1); + } + + System.out.println(dfs(0, 0)); + } + + public static int dfs(int y, int x) { + // 인덱스 범위 벗어나면 0 + if (y >= s1.length() || x >= s2.length()) return 0; + + if (dp[y][x] != -1) return dp[y][x]; // 이미 계산했으면 반환 + + int result; + if (s1.charAt(y) == s2.charAt(x)) { + // 두 문자가 같으면 같이 한 칸씩 이동 + result = 1 + dfs(y + 1, x + 1); + } else { + // 다르면 한쪽만 이동 (두 가지 선택 중 큰 값) + result = Math.max(dfs(y + 1, x), dfs(y, x + 1)); + } + + return dp[y][x] = result; + } +} From ef6af8ba30aa29345766cc7cc9bdeb5451700c1c Mon Sep 17 00:00:00 2001 From: donghyeon Date: Wed, 8 Oct 2025 23:42:55 +0900 Subject: [PATCH 3/6] =?UTF-8?q?feat:=20beakjoon=201446/=EC=A7=80=EB=A6=84?= =?UTF-8?q?=EA=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BaekJoon/1446/src/donghyeon95.java | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 BaekJoon/1446/src/donghyeon95.java diff --git a/BaekJoon/1446/src/donghyeon95.java b/BaekJoon/1446/src/donghyeon95.java new file mode 100644 index 0000000..d1940fc --- /dev/null +++ b/BaekJoon/1446/src/donghyeon95.java @@ -0,0 +1,65 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; +import java.util.stream.Collectors; + +public class Main { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int D = Integer.parseInt(st.nextToken()); + Set set = new TreeSet<>(); + + // 2차원 배열 대신 Map 사용 (메모리 절감) + Map[] distArr = new HashMap[10001]; + for (int i = 0; i <= 10000; i++) distArr[i] = new HashMap<>(); + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + int start = Integer.parseInt(st.nextToken()); + int end = Integer.parseInt(st.nextToken()); + int dist = Integer.parseInt(st.nextToken()); + + if (end > D) continue; + + set.add(start); + set.add(end); + + // 최소 거리 유지 (distArr[start].put(end, dist)) + distArr[start].merge(end, dist, Math::min); + } + + set.add(D); + + var sortedSet = set.stream().sorted().collect(Collectors.toList()); + + int[] answers = new int[D + 1]; + Arrays.fill(answers, Integer.MAX_VALUE); + answers[0] = 0; + + int lastedNode = 0; + + for (int node : sortedSet) { + for (int i = 0; i <= node; i++) { // N → D + if (distArr[i].containsKey(node)) { + int dist = distArr[i].get(node); + if (answers[i] == Integer.MAX_VALUE) + answers[node] = Math.min(answers[node], dist); + else + answers[node] = Math.min(anslwers[node], answers[i] + dist); + } + } + + if (answers[lastedNode] == Integer.MAX_VALUE) + answers[node] = Math.min(answers[node], node - lastedNode); + else + answers[node] = Math.min(answers[node], answers[lastedNode] + (node - lastedNode)); + + lastedNode = node; + } + + System.out.println(answers[D]); + } +} From 275ef30573825ab5cb7ed4fdfc1d0da8a50d81b1 Mon Sep 17 00:00:00 2001 From: donghyeon Date: Mon, 13 Oct 2025 21:52:55 +0900 Subject: [PATCH 4/6] =?UTF-8?q?feat:=20beakjoon=201162.=20=EB=8F=84?= =?UTF-8?q?=EB=A1=9C=20=ED=8F=AC=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../donghyeon95.java" | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 "BaekJoon/1162/\353\217\204\353\241\234 \355\217\254\354\236\245/donghyeon95.java" diff --git "a/BaekJoon/1162/\353\217\204\353\241\234 \355\217\254\354\236\245/donghyeon95.java" "b/BaekJoon/1162/\353\217\204\353\241\234 \355\217\254\354\236\245/donghyeon95.java" new file mode 100644 index 0000000..1aaf513 --- /dev/null +++ "b/BaekJoon/1162/\353\217\204\353\241\234 \355\217\254\354\236\245/donghyeon95.java" @@ -0,0 +1,71 @@ +import java.io.*; +import java.util.*; + +public class Main { + static class Node implements Comparable { + int idx, used, cost; + Node(int idx, int used, int cost) { + this.idx = idx; + this.used = used; // 포장 사용 횟수 + this.cost = cost; + } + @Override + public int compareTo(Node o) { + return Integer.compare(this.cost, o.cost); + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + int N = Integer.parseInt(st.nextToken()); + int M = Integer.parseInt(st.nextToken()); + int K = Integer.parseInt(st.nextToken()); + + List[] graph = new ArrayList[N + 1]; + for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>(); + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + graph[a].add(new int[]{b, w}); + graph[b].add(new int[]{a, w}); + } + + // dp[node][k] = node까지 k개 포장했을 때 최소 비용 + long[][] dp = new long[N + 1][K + 1]; + for (int i = 0; i <= N; i++) Arrays.fill(dp[i], Long.MAX_VALUE); + dp[1][0] = 0; + + PriorityQueue pq = new PriorityQueue<>(); // 원래는 간선이 짧은 순서지만 지금은 현재까지 중에서 Cost가 작은 순서대로 + pq.offer(new Node(1, 0, 0)); + + while (!pq.isEmpty()) { + Node cur = pq.poll(); + if (dp[cur.idx][cur.used] < cur.cost) continue; + + for (int[] next : graph[cur.idx]) { + int nextNode = next[0]; + int weight = next[1]; + + // 포장 안함 + if (dp[nextNode][cur.used] > dp[cur.idx][cur.used] + weight) { + dp[nextNode][cur.used] = dp[cur.idx][cur.used] + weight; + pq.offer(new Node(nextNode, cur.used, (int) dp[nextNode][cur.used])); + } + + // 포장 함 + if (cur.used < K && dp[nextNode][cur.used + 1] > dp[cur.idx][cur.used]) { + dp[nextNode][cur.used + 1] = dp[cur.idx][cur.used]; + pq.offer(new Node(nextNode, cur.used + 1, (int) dp[nextNode][cur.used + 1])); + } + } + } + + long ans = Long.MAX_VALUE; + for (int i = 0; i <= K; i++) ans = Math.min(ans, dp[N][i]); + System.out.println(ans); + } +} From 049fc60762d0a76b2a52f7059e5118fdbc5d870f Mon Sep 17 00:00:00 2001 From: donghyeon Date: Mon, 13 Oct 2025 23:00:26 +0900 Subject: [PATCH 5/6] =?UTF-8?q?feat:=20beakjoon=2014500.=20=ED=85=8C?= =?UTF-8?q?=ED=8A=B8=EB=A1=9C=EB=AF=B8=EB=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../donghyeon95.java" | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 "BaekJoon/14500/\355\205\214\355\212\270\353\241\234\353\257\270\353\205\270/donghyeon95.java" diff --git "a/BaekJoon/14500/\355\205\214\355\212\270\353\241\234\353\257\270\353\205\270/donghyeon95.java" "b/BaekJoon/14500/\355\205\214\355\212\270\353\241\234\353\257\270\353\205\270/donghyeon95.java" new file mode 100644 index 0000000..f4d4196 --- /dev/null +++ "b/BaekJoon/14500/\355\205\214\355\212\270\353\241\234\353\257\270\353\205\270/donghyeon95.java" @@ -0,0 +1,79 @@ +import java.io.*; +import java.util.*; + +public class Main { + static int N, M; + static int[][] arr; + static boolean[][] visited; + static int max = 0; + static int[] dx = {1, -1, 0, 0}; + static int[] dy = {0, 0, 1, -1}; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + arr = new int[N][M]; + visited = new boolean[N][M]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < M; j++) { + arr[i][j] = Integer.parseInt(st.nextToken()); + } + } + + for (int i = 0; i < N; i++) { + for (int j = 0; j < M; j++) { + visited[i][j] = true; + dfs(i, j, arr[i][j], 1); + visited[i][j] = false; + checkT(i, j); // 'ㅗ' 모양 예외 처리 + } + } + + System.out.println(max); + } + + static void dfs(int x, int y, int sum, int cnt) { + if (cnt == 4) { + max = Math.max(max, sum); + return; + } + for (int d = 0; d < 4; d++) { + int nx = x + dx[d]; + int ny = y + dy[d]; + if (nx < 0 || ny < 0 || nx >= N || ny >= M) continue; + if (!visited[nx][ny]) { + visited[nx][ny] = true; + dfs(nx, ny, sum + arr[nx][ny], cnt + 1); + visited[nx][ny] = false; + } + } + } + + static void checkT(int x, int y) { + int[][] t = { + {0, -1, 0, 1, -1, 0}, + {0, -1, 0, 1, 1, 0}, + {-1, 0, 1, 0, 0, 1}, + {-1, 0, 1, 0, 0, -1} + }; + + for (int[] shape : t) { + int sum = arr[x][y]; + boolean valid = true; + for (int i = 0; i < 3; i++) { + int nx = x + shape[i * 2]; + int ny = y + shape[i * 2 + 1]; + if (nx < 0 || ny < 0 || nx >= N || ny >= M) { + valid = false; + break; + } + sum += arr[nx][ny]; + } + if (valid) max = Math.max(max, sum); + } + } +} From 65ca7c9ff376946db8151822754bda1e83c28f29 Mon Sep 17 00:00:00 2001 From: donghyeon95 Date: Tue, 14 Oct 2025 13:47:10 +0900 Subject: [PATCH 6/6] =?UTF-8?q?[donghyeon95]=20beakjoon=207812.=20?= =?UTF-8?q?=EC=A4=91=EC=95=99=20=ED=8A=B8=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../donghyeon95.java" | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 "BaekJoon/7812/ \354\244\221\354\225\231\355\212\270\353\246\254/donghyeon95.java" diff --git "a/BaekJoon/7812/ \354\244\221\354\225\231\355\212\270\353\246\254/donghyeon95.java" "b/BaekJoon/7812/ \354\244\221\354\225\231\355\212\270\353\246\254/donghyeon95.java" new file mode 100644 index 0000000..ccfe443 --- /dev/null +++ "b/BaekJoon/7812/ \354\244\221\354\225\231\355\212\270\353\246\254/donghyeon95.java" @@ -0,0 +1,81 @@ +import java.io.*; +import java.util.*; + +public class Main { + private static long[] sums; // 거리합 (long) + private static int[] childCnt; // 서브트리 크기 + private static Map> graph; + private static int N; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + while (true) { + st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + if (N == 0) break; + + sums = new long[N]; + childCnt = new int[N]; + graph = new HashMap<>(); + + // 양방향 그래프 초기화 + for (int i = 0; i < N - 1; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int w = Integer.parseInt(st.nextToken()); + + graph.putIfAbsent(a, new ArrayList<>()); + graph.putIfAbsent(b, new ArrayList<>()); + + graph.get(a).add(new int[]{b, w}); + graph.get(b).add(new int[]{a, w}); + } + + // 루트(0) 기준 거리합 계산 + initDfs(0, -1); + + // 다른 루트로 이동하면서 거리합 갱신 + calcDfs(0, -1); + + // 최소 거리합 출력 + long answer = Arrays.stream(sums).min().getAsLong(); + System.out.println(answer); + } + } + + // 루트 기준 초기 거리합 계산 + public static int initDfs(int node, int parent) { + int cnt = 1; + long total = 0; + + for (int[] edge : graph.getOrDefault(node, new ArrayList<>())) { + int next = edge[0]; + int w = edge[1]; + if (next == parent) continue; // 부모 방지 + + int childCount = initDfs(next, node); + total += sums[next] + (long) w * childCount; + cnt += childCount; + } + + sums[node] = total; + childCnt[node] = cnt; + return cnt; + } + + // 루트를 자식으로 바꾸며 거리합 갱신 + private static void calcDfs(int node, int parent) { + for (int[] edge : graph.getOrDefault(node, new ArrayList<>())) { + int next = edge[0]; + int w = edge[1]; + if (next == parent) continue; + + // 부모에서 자식으로 루트 이동 시 변화량 + sums[next] = sums[node] + (long) (N - 2 * childCnt[next]) * w; + calcDfs(next, node); + } + } +}