From a529e835d9b4e8362a9839d63e85bb2fde657753 Mon Sep 17 00:00:00 2001 From: KodaHye Date: Mon, 4 Nov 2024 10:04:19 +0900 Subject: [PATCH 1/3] =?UTF-8?q?=EA=B3=A0=EB=8B=A4=ED=98=9C:=20[CT]=20?= =?UTF-8?q?=EC=A0=84=ED=88=AC=EB=A1=9C=EB=B4=87=5F241104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\355\210\254\353\241\234\353\264\207.java" | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 "CodeTree/2017-2018\353\205\204/DH_\354\240\204\355\210\254\353\241\234\353\264\207.java" diff --git "a/CodeTree/2017-2018\353\205\204/DH_\354\240\204\355\210\254\353\241\234\353\264\207.java" "b/CodeTree/2017-2018\353\205\204/DH_\354\240\204\355\210\254\353\241\234\353\264\207.java" new file mode 100644 index 0000000..f6847ad --- /dev/null +++ "b/CodeTree/2017-2018\353\205\204/DH_\354\240\204\355\210\254\353\241\234\353\264\207.java" @@ -0,0 +1,152 @@ +import java.io.*; +import java.util.*; + +public class DH_전투로봇 { + static class Point { + int r, c; + public Point(int r, int c) { + this.r = r; + this.c = c; + } + } + + static class qPoint implements Comparable { + Point p; + int d; + public qPoint(Point p, int d) { + this.p = p; + this.d = d; + } + + @Override + public int compareTo(qPoint o) { + if(this.d == o.d) { + if(this.p.r == o.p.r) return Integer.compare(this.p.c, o.p.c); // 열 오름차순 + return Integer.compare(this.p.r, o.p.r); // 행 오름차순s + } + return Integer.compare(this.d, o.d); // 거리 오름차순 + } + } + + static int[] dr = {-1, 1, 0, 0}, dc = {0, 0, -1, 1}; + static int N, dieMonsterCnt, totalTime; + static int[][] levelMap, monsterIdxMap; + static Point robot; + + public static void main(String[] args) throws Exception { + initInput(); + solution(); + } + + static void solution() { + + int time; + while((time = bfs()) != 0) { + totalTime += time; + } + System.out.println(totalTime); + } + + static int bfs() { + + PriorityQueue q = new PriorityQueue<>(); + boolean[][] v = new boolean[N][N]; + + q.add(new qPoint(robot, 0)); + v[robot.r][robot.c] = true; + int robotLevel = levelMap[robot.r][robot.c]; + levelMap[robot.r][robot.c] = 0; + + // 없앨 수 있는 몬스터 정보 구하기 + while(!q.isEmpty()) { + qPoint current = q.poll(); + + // 몬스터를 없앨 수 있다면 + if(canKillMonster(current.p, robotLevel)) { + // 몬스터 없앤 개수 늘리기 + dieMonsterCnt++; + // 없앤 몬스터의 개수와 로봇의 레벨이 같다면 + if(dieMonsterCnt == robotLevel) { + // 로봇의 레벨 늘려주기 + dieMonsterCnt = 0; + robotLevel++; + } + + // map에 있는 몬스터의 정보 없애주기 + levelMap[current.p.r][current.p.c] = 0; + monsterIdxMap[current.p.r][current.p.c] = 0; + + // 로봇 위치, map에 로봇 정보 갱신하기 + robot.r = current.p.r; + robot.c = current.p.c; + levelMap[current.p.r][current.p.c] = robotLevel; + + // 로봇이 이동한 위치 반환 + return current.d; + } + + for(int d = 0; d < 4; d++) { + int nr = current.p.r + dr[d]; + int nc = current.p.c + dc[d]; + + // 범위를 벗어나거나 || 이미 방문했던 곳이거나 || 자신의 레벨보다 큰 몬스터가 있거나 + if(!check(nr, nc) || v[nr][nc] || levelMap[nr][nc] > robotLevel) continue; + q.add(new qPoint(new Point(nr, nc), current.d + 1)); + v[nr][nc] = true; + } + } + + // 몬스터를 해치우지 못한 경우 + return 0; + } + + static boolean canKillMonster(Point current, int robotLevel) { + int monsterLevel = levelMap[current.r][current.c]; + return 0 < monsterLevel && monsterLevel < robotLevel; + } + + static boolean check(int r, int c) { + return 0 <= r && r < N && 0 <= c && c < N; + } + + static void initInput() throws Exception { + System.setIn(new FileInputStream("../AlgorithmStudy/input/전투로봇.txt")); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + N = Integer.parseInt(br.readLine()); + levelMap = new int[N][N]; + monsterIdxMap = new int[N][N]; + + int idx = 1; + for(int r = 0; r < N; r++) { + st = new StringTokenizer(br.readLine()); + for(int c = 0; c < N; c++) { + levelMap[r][c] = Integer.parseInt(st.nextToken()); + if(levelMap[r][c] == 9) { + robot = new Point(r, c); + levelMap[r][c] = 2; // 전투로봇의 초기 레벨: 2 + } + else if(levelMap[r][c] != 0) { + monsterIdxMap[r][c] = idx; + } + } + } + +// printRobotInfo(); +// printlevelMapInfo(); +// printMostersInfo(); + } + + static void printRobotInfo() { + System.out.println("robotInfo: " + robot.toString() + ", level: " + levelMap[robot.r][robot.c]); + } + + static void printlevelMapInfo() { + for(int r = 0; r < N; r++) { + System.out.println(Arrays.toString(levelMap[r])); + } + + System.out.println(); + } +} From 62d75ab6307a256edb805adc6bfb16143ddd408e Mon Sep 17 00:00:00 2001 From: KodaHye Date: Mon, 4 Nov 2024 14:02:35 +0900 Subject: [PATCH 2/3] =?UTF-8?q?=EA=B3=A0=EB=8B=A4=ED=98=9C:=20[PG]=2064062?= =?UTF-8?q?=20=EC=A7=95=EA=B2=80=EB=8B=A4=EB=A6=AC=20=EA=B1=B4=EB=84=88?= =?UTF-8?q?=EA=B8=B0=5F241104?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Programmers/Level3/DH_64062.java | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Programmers/Level3/DH_64062.java diff --git a/Programmers/Level3/DH_64062.java b/Programmers/Level3/DH_64062.java new file mode 100644 index 0000000..5f7c5bc --- /dev/null +++ b/Programmers/Level3/DH_64062.java @@ -0,0 +1,39 @@ + +/* + * 징검다리 건너기 + */ + +public class DH_64062 { + static int solution(int[] stones, int k) { + // 돌을 건넌 사람의 최대 수 구하기 - upper bound 구하기 + int s = 0, e = 200_000_001; + + while(s <= e) { + // 돌을 건넌 친구 수 + int m = (s + e) / 2; + + int cnt = 0; + + // 친구들이 모두 길을 건넜을 때 + // 값이 0이하가 되는 곳이 k개 이상이 된다면 못건너감 + boolean canGo = true; + for(int stone: stones) { + if(stone - m < 0) cnt++; + else cnt = 0; + + if(cnt >= k) canGo = false; + } + + if(canGo) s = m + 1; + else e = m - 1; + } + + return e; + } + + public static void main(String[] args) { + int[] stones = {2, 4, 5, 3, 2, 1, 4, 2, 5, 1}; + int k = 3; + System.out.println(solution(stones, k)); + } +} From cc2e04bd49111b1fd8911d43618bf700beab5b77 Mon Sep 17 00:00:00 2001 From: KodaHye Date: Tue, 5 Nov 2024 10:38:19 +0900 Subject: [PATCH 3/3] =?UTF-8?q?=EA=B3=A0=EB=8B=A4=ED=98=9C:=20[BOJ]=202660?= =?UTF-8?q?=20=ED=9A=8C=EC=9E=A5=EB=BD=91=EA=B8=B0=5F241105?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "BOJ/1000-5000\353\262\210/DH_2660.java" | 78 ++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 "BOJ/1000-5000\353\262\210/DH_2660.java" diff --git "a/BOJ/1000-5000\353\262\210/DH_2660.java" "b/BOJ/1000-5000\353\262\210/DH_2660.java" new file mode 100644 index 0000000..b4e9067 --- /dev/null +++ "b/BOJ/1000-5000\353\262\210/DH_2660.java" @@ -0,0 +1,78 @@ +import java.io.*; +import java.util.*; + +/* + * 회장뽑기 + */ + +public class DH_2660 { + static int N, maxDepth; + static ArrayList adj[]; + static ArrayDeque q; + static boolean[] v; + static int[] depth; // 각 회원마다 끝까지 탐색했을 때 depth를 저장할 변수 + static TreeSet set; // 회장이 될 수 있는 사람의 번호를 담음 + + public static void main(String[] args) throws Exception { + initInput(); + solution(); + + StringBuilder sb = new StringBuilder(); + + sb.append(maxDepth).append(" ").append(set.size()).append("\n"); + for(int i: set) sb.append(i).append(" "); + System.out.println(sb); + } + + static void solution() { + for(int i = 1; i < adj.length; i++) { + v = new boolean[N + 1]; + q.add(new int[] {i, 0}); // [0]: 학생 번호, [1]: depth 깊이 + v[i] = true; + + while(!q.isEmpty()) { + int[] current = q.poll(); + depth[i] = current[1]; + + for(int next: adj[current[0]]) { + if(v[next]) continue; + q.add(new int[] {next, current[1] + 1}); + v[next] = true; + } + } + + if(depth[i] < maxDepth) { // maxDepth 깊이가 작아야 점수가 작아지기 때문에 + maxDepth = depth[i]; + set.clear(); + } + + if(depth[i] == maxDepth) set.add(i); + } + } + + static void initInput() throws Exception { + System.setIn(new FileInputStream("../AlgorithmStudy/input/BOJ2660.txt")); + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st; + + q = new ArrayDeque<>(); + set = new TreeSet(); + + maxDepth = Integer.MAX_VALUE; + N = Integer.parseInt(br.readLine()); + adj = new ArrayList[N + 1]; + for(int i = 0; i < adj.length; i++) adj[i] = new ArrayList(); + + depth = new int[N + 1]; + + String s; + while(!(s = br.readLine()).equals("-1 -1")) { + st = new StringTokenizer(s); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + + adj[a].add(b); + adj[b].add(a); + } + } +}