From 86b5361d21dc32e0c96755d1d8b4eecc964b3b64 Mon Sep 17 00:00:00 2001 From: "YeJun, Jung" Date: Sat, 7 Mar 2026 23:26:33 +0900 Subject: [PATCH 1/2] feat: add boj p2458 solution --- problems/baekjoon/p2458/Main.java | 132 ++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 problems/baekjoon/p2458/Main.java diff --git a/problems/baekjoon/p2458/Main.java b/problems/baekjoon/p2458/Main.java new file mode 100644 index 0000000..2b1de26 --- /dev/null +++ b/problems/baekjoon/p2458/Main.java @@ -0,0 +1,132 @@ +/* + * (2458) 키 순서 + * https://www.acmicpc.net/problem/2458 + */ + +import java.io.*; +import java.util.*; + +/** + * Baekjoon - 키 순서 + * @author YeJun, Jung + * + * [분석] + * - 학생은 1~N까지 번호가 매겨져 있으며 학생의 키는 모두 다르다. + * - 문제 예시에서 힌트를 얻을 수 있었다. + * - "4번 학생은 자기보다 작은 학생이 3명이 있고, 자기보다 큰 학생이 2명이 있게 되어 자신의 키가 몇 번째인지 정확히 알 수 있다." + * - 특정 학생을 기준으로 학생보다 (작은 학생 수) + (큰 학생 수) + 1 == (전체 학생 수) 조건을 만족한다면 + * - 해당 학생은 자신의 키가 몇 번째인지 정확히 알 수 있다. + * + * [전략] + * - 학생들의 키 정보를 인접리스트로 저장한다. + * - 자신보다 큰 학생의 번호는 그대로 저장하고, 자신보다 작은 학생의 번호는 (-번호)로 저장한다. + * - 1~N 학생들에 대해서... + * - 자신보다 큰 학생들의 수를 DFS로 카운트하고 + * - 자신보다 작은 학생들의 수를 DFS로 카운트 하여 더한다. + * - 또한 자기자신에 대해서 +1 하여 더한다. + * - 구한 값이 (전체 학생 수)와 같으면 해당 학생의 키 순서는 정확히 알 수 있다. + * - 정답 변수를 업데이트 한다. + */ +public class Main { + static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder output = new StringBuilder(); + static StringTokenizer input; + + // -------------------------------------------------------- + + public static void main(String[] args) throws IOException { + new Main().run(); + System.out.print(output.toString()); + } + + // -------------------------------------------------------- + + int answer; + int nodeLen; + int edgeLen; + List> graph; + boolean[] visited; + + public void run() throws IOException { input(); solve(); print(); } + + private void input() throws IOException { + int a, b; + + nodeLen = nextInt(); + edgeLen = nextInt(); + + graph = new ArrayList<>(nodeLen + 1); + for (int idx = 0; idx <= nodeLen; idx++) { + graph.add(new ArrayList<>()); + } + + for (int idx = 0; idx < edgeLen; idx++) { + a = nextInt(); + b = nextInt(); + + graph.get(a).add(b); // a < b + graph.get(b).add(-a); // a < b + } + } + + private void solve() { + answer = 0; + + // 각 노드에서 출발하는 DFS 탐색 하면서... + // 자신의 (뒤에 있는 노드) + (앞에 있는 노드) + 1 == nodeLen 판별 + for (int node = 1; node <= nodeLen; node++) { + visited = new boolean[nodeLen + 1]; + visited[node] = true; + + int cnt = 1; + cnt += dfs_forward(node); + cnt += dfs_backward(node); + + if (cnt == nodeLen) answer++; + } + } + + private int dfs_forward(int node) { + int result = 0; + + for (int nnode : graph.get(node)) { + if (nnode < 0 || visited[nnode]) continue; + + visited[nnode] = true; + result += 1 + dfs_forward(nnode); + } + + return result; + } + + private int dfs_backward(int node) { + int result = 0; + + for (int nnode : graph.get(node)) { + if (nnode > 0 || visited[-nnode]) continue; + + visited[-nnode] = true; + result += 1 + dfs_backward(-nnode); + } + + return result; + } + + private void print() { + output + .append(answer) + .append('\n'); + } + + // -------------------------------------------------------- + + static String next() throws IOException { + if (input == null || !input.hasMoreTokens()) + input = new StringTokenizer(reader.readLine().trim()); + return input.nextToken(); + } + + static int nextInt() throws IOException { + return Integer.parseInt(next()); + } +} From 1e3213322c9789baafc41d7f19a6a370de7f05fb Mon Sep 17 00:00:00 2001 From: "YeJun, Jung" Date: Sat, 7 Mar 2026 23:26:52 +0900 Subject: [PATCH 2/2] feat: add swea p1767v2 solution --- problems/SWEA/p1767v2/Solution.java | 277 ++++++++++++++++++++++++++++ 1 file changed, 277 insertions(+) create mode 100644 problems/SWEA/p1767v2/Solution.java diff --git a/problems/SWEA/p1767v2/Solution.java b/problems/SWEA/p1767v2/Solution.java new file mode 100644 index 0000000..2e59c5d --- /dev/null +++ b/problems/SWEA/p1767v2/Solution.java @@ -0,0 +1,277 @@ +/* + * (1767) [SW Test 샘플문제] 프로세서 연결하기 + * https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV4suNtaXFEDFAUf&categoryId=AV4suNtaXFEDFAUf&categoryType=CODE&problemTitle=1767&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1 + */ +import java.io.*; +import java.util.*; + +public class Solution { + static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); + static StringBuilder output = new StringBuilder(); + static StringTokenizer input; + + // ---------------------------------------------------------- + + public static void main(String[] args) throws IOException { + final int testCount = nextInt(); + + for (int testCase = 1; testCase <= testCount; testCase++) { + new Solution(testCase).run(); + } + + System.out.print(output.toString()); + } + + // ---------------------------------------------------------- + + static final int[] DIR_X = { -1, 1, 0, 0 }; + static final int[] DIR_Y = { 0, 0, -1, 1 }; + static final int[] REVERSE = { 1, 0, 3, 2 }; + static final int DIR_LEN = 4; + + int testCase; + int answer; + int boardSize; + int[][] board; + List coreList; + int coreLen; + + public Solution(int testCase) { this.testCase = testCase; } + public void run() throws IOException { input(); solve(); print(); } + + private void input() throws IOException { + coreList = new ArrayList<>(); + boardSize = nextInt(); + board = new int[boardSize][boardSize]; + + for (int y = 0; y < boardSize; y++) { + for (int x = 0; x < boardSize; x++) { + board[y][x] = nextInt(); + + if (board[y][x] == 1 && + x != 0 && x != boardSize - 1 && + y != 0 && y != boardSize - 1 + ) { + coreList.add(new Pos(x, y)); + } + } + } + + coreLen = coreList.size(); + } + + private void solve() { + answer = dfs(0, 0, 0).len; + } + + private Result dfs(int core, int cnt, int len) { + if (core == coreLen) return new Result(cnt, len); + + Pos pos = coreList.get(core); + + // 수정된 버전: 속도가 더 빠르다. + // 4방향 탐색 성공 여부와 관계 없이 무조건 해당 코어를 배제한 경우도 탐색해야 함 + Result result = dfs(core + 1, cnt, len); + + for (int dir = 0; dir < DIR_LEN; dir++) { + int nx = pos.x + DIR_X[dir]; + int ny = pos.y + DIR_Y[dir]; + + int current = 0; + while (isInsideBoard(nx, ny) && board[ny][nx] == 0) { + board[ny][nx] = 2; + + current++; + + nx += DIR_X[dir]; + ny += DIR_Y[dir]; + } + + if (!isInsideBoard(nx, ny) && current != 0) { + result = Result.selectBest(result, dfs(core + 1, cnt + 1, len + current)); + } + + undoBoard(nx, ny, dir, current); + } + + return result; + } + + private void undoBoard(int x, int y, int dir, int len) { + int rdir = REVERSE[dir]; + + for (int cnt = 0; cnt < len; cnt++) { + x += DIR_X[rdir]; + y += DIR_Y[rdir]; + + board[y][x] = 0; + } + } + + private boolean isInsideBoard(int x, int y) { + return x >= 0 && x < boardSize && y >= 0 && y < boardSize; + } + + private void print() { + output + .append('#') + .append(testCase) + .append(' ') + .append(answer) + .append('\n'); + } + + // ---------------------------------------------------------- + + static class Pos { + int x, y; + + public Pos(int x, int y) { + this.x = x; + this.y = y; + } + } + + // ---------------------------------------------------------- + + static class Result { + int cnt, len; + + public Result(int cnt, int len) { + this.cnt = cnt; + this.len = len; + } + + public Result() { + this(0, Integer.MAX_VALUE); + } + + public static Result selectBest(Result a, Result b) { + if (a.cnt == b.cnt) { + if (a.len < b.len) { + return a; + } else { + return b; + } + } else if (a.cnt > b.cnt) { + return a; + } else { + return b; + } + } + } + + // ---------------------------------------------------------- + + static String next() throws IOException { + if (input == null || !input.hasMoreTokens()) + input = new StringTokenizer(reader.readLine().trim()); + return input.nextToken(); + } + + static int nextInt() throws IOException { + return Integer.parseInt(next()); + } +} + + + + + +/* + +[예시 입력] +9 +7 +0 0 1 0 0 0 0 +0 0 1 0 0 0 0 +0 0 0 0 0 1 0 +0 0 0 0 0 0 0 +1 1 0 1 0 0 0 +0 1 0 0 0 0 0 +0 0 0 0 0 0 0 +9 +0 0 0 0 0 0 0 0 0 +0 0 1 0 0 0 0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