From 9eaee6e35e0636d01108b7c2a18f6ced5713e9f0 Mon Sep 17 00:00:00 2001 From: GetMapping Date: Mon, 7 Mar 2022 08:29:57 +0900 Subject: [PATCH 1/5] unsolve: BOJ_16234 --- eonju/BOJ/BOJ_1238.java | 103 +++++++++++++++++++++++++++++++++++ eonju/BOJ/BOJ_16234.java | 115 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 218 insertions(+) create mode 100644 eonju/BOJ/BOJ_1238.java create mode 100644 eonju/BOJ/BOJ_16234.java diff --git a/eonju/BOJ/BOJ_1238.java b/eonju/BOJ/BOJ_1238.java new file mode 100644 index 0000000..62dff90 --- /dev/null +++ b/eonju/BOJ/BOJ_1238.java @@ -0,0 +1,103 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.PriorityQueue; + +class BOJ_1238 { + + private static int N; + private static int M; + private static int X; + private static HashMap> graph; + private static int[] dist; + + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String[] input = bufferedReader.readLine().split(" "); + N = Integer.parseInt(input[0]); + M = Integer.parseInt(input[1]); + X = Integer.parseInt(input[2]); + + graph = new HashMap<>(); + for (int i = 1; i <= N; i++) { + graph.put(i, new ArrayList<>()); + } + + for (int i = 0; i < M; i++) { + input = bufferedReader.readLine().split(" "); + int targetA = Integer.parseInt(input[0]); + int targetB = Integer.parseInt(input[1]); + int weight = Integer.parseInt(input[2]); + + graph.get(targetA).add(new Edge(targetB, weight)); + } + + dist = new int[N + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dijkstra(X); + int[] answers = dist.clone(); + + for (int i = 1; i <= N; i++) { + Arrays.fill(dist, Integer.MAX_VALUE); + dijkstra(i); + answers[i] += dist[X]; + } + + int max = answers[1]; + for (int i = 1; i <= N; i++) { + if (max < answers[i]) { + max = answers[i]; + } + } + + System.out.println(max); + } + + public static void dijkstra(int start) { + PriorityQueue queue = new PriorityQueue<>(Comparator.comparingInt(Edge::getWeight)); + + dist[start] = 0; + queue.add(new Edge(start, 0)); + + while (!queue.isEmpty()) { + Edge poll = queue.poll(); + + if (poll.getWeight() > dist[poll.getEnd()]) { + continue; + } + + List edges = graph.get(poll.getEnd()); + for (Edge edge : edges) { + if (poll.getWeight() + edge.getWeight() < dist[edge.getEnd()]) { + dist[edge.getEnd()] = poll.getWeight() + edge.getWeight(); + queue.add(new Edge(edge.getEnd(), dist[edge.getEnd()])); + } + } + } + } + + static class Edge { + + private int end; + private int weight; + + public Edge(int end, int weight) { + this.end = end; + this.weight = weight; + } + + public int getEnd() { + return end; + } + + public int getWeight() { + return weight; + } + } +} diff --git a/eonju/BOJ/BOJ_16234.java b/eonju/BOJ/BOJ_16234.java new file mode 100644 index 0000000..66880c6 --- /dev/null +++ b/eonju/BOJ/BOJ_16234.java @@ -0,0 +1,115 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; + +class BOJ_16234 { + + private static int[] moveI = {-1, 1, 0, 0}; + private static int[] moveJ = {0, 0, -1, 1}; + private static int[][] map; + private static int[][] copyMap; + private static boolean[][] visited; + private static boolean flag; + private static int N; + private static int L; + private static int R; + + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String[] input = bufferedReader.readLine().split(" "); + N = Integer.parseInt(input[0]); + L = Integer.parseInt(input[1]); + R = Integer.parseInt(input[2]); + + map = new int[N][N]; + visited = new boolean[N][N]; + + for (int i = 0; i < N; i++) { + input = bufferedReader.readLine().split(" "); + for (int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(input[j]); + } + } + + int year = 0; + while (true) { + flag = false; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!visited[i][j]) { + copyMap = new int[N][N]; + visited = new boolean[N][N]; + bfs(i, j); + } + + } + } + + if (flag) { + year++; + } else { + break; + } + } + + System.out.println(year); + + } + + public static void bfs(int startI, int startJ) { + Queue queue = new LinkedList<>(); + visited[startI][startJ] = true; + queue.add(new int[]{startI, startJ}); + + int sum = map[startI][startJ]; + int cnt = 1; + + while (!queue.isEmpty()) { + int[] poll = queue.poll(); + int nowI = poll[0]; + int nowJ = poll[1]; + + for (int i = 0; i < 4; i++) { + int nextI = nowI + moveI[i]; + int nextJ = nowJ + moveJ[i]; + + if (nextI < 0 || nextJ < 0 || nextI >= N || nextJ >= N) { + continue; + } + + if (visited[nextI][nextJ]) { + continue; + } + + if (isInPopulation(map[nowI][nowJ] - map[nextI][nextJ])) { + queue.add(new int[]{nextI, nextJ}); + + copyMap[nextI][nextJ] = map[nextI][nextJ]; + sum += map[nextI][nextJ]; + cnt++; + flag = true; + } + } + } + + movePopulation(sum, cnt); + } + + public static void movePopulation(int sum, int cnt) { + int people = sum / cnt; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (copyMap[i][j] > 0) { + map[i][j] = people; + } + } + } + } + + public static boolean isInPopulation(int number) { + return number >= L && number <= R; + } +} From 5c62c63b0dae132aaceed99456b0b4c3d0e71532 Mon Sep 17 00:00:00 2001 From: GetMapping Date: Mon, 7 Mar 2022 08:31:01 +0900 Subject: [PATCH 2/5] Revert "unsolve: BOJ_16234" This reverts commit 9eaee6e35e0636d01108b7c2a18f6ced5713e9f0. --- eonju/BOJ/BOJ_1238.java | 103 ----------------------------------- eonju/BOJ/BOJ_16234.java | 115 --------------------------------------- 2 files changed, 218 deletions(-) delete mode 100644 eonju/BOJ/BOJ_1238.java delete mode 100644 eonju/BOJ/BOJ_16234.java diff --git a/eonju/BOJ/BOJ_1238.java b/eonju/BOJ/BOJ_1238.java deleted file mode 100644 index 62dff90..0000000 --- a/eonju/BOJ/BOJ_1238.java +++ /dev/null @@ -1,103 +0,0 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.PriorityQueue; - -class BOJ_1238 { - - private static int N; - private static int M; - private static int X; - private static HashMap> graph; - private static int[] dist; - - public static void main(String[] args) throws IOException { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); - String[] input = bufferedReader.readLine().split(" "); - N = Integer.parseInt(input[0]); - M = Integer.parseInt(input[1]); - X = Integer.parseInt(input[2]); - - graph = new HashMap<>(); - for (int i = 1; i <= N; i++) { - graph.put(i, new ArrayList<>()); - } - - for (int i = 0; i < M; i++) { - input = bufferedReader.readLine().split(" "); - int targetA = Integer.parseInt(input[0]); - int targetB = Integer.parseInt(input[1]); - int weight = Integer.parseInt(input[2]); - - graph.get(targetA).add(new Edge(targetB, weight)); - } - - dist = new int[N + 1]; - Arrays.fill(dist, Integer.MAX_VALUE); - - dijkstra(X); - int[] answers = dist.clone(); - - for (int i = 1; i <= N; i++) { - Arrays.fill(dist, Integer.MAX_VALUE); - dijkstra(i); - answers[i] += dist[X]; - } - - int max = answers[1]; - for (int i = 1; i <= N; i++) { - if (max < answers[i]) { - max = answers[i]; - } - } - - System.out.println(max); - } - - public static void dijkstra(int start) { - PriorityQueue queue = new PriorityQueue<>(Comparator.comparingInt(Edge::getWeight)); - - dist[start] = 0; - queue.add(new Edge(start, 0)); - - while (!queue.isEmpty()) { - Edge poll = queue.poll(); - - if (poll.getWeight() > dist[poll.getEnd()]) { - continue; - } - - List edges = graph.get(poll.getEnd()); - for (Edge edge : edges) { - if (poll.getWeight() + edge.getWeight() < dist[edge.getEnd()]) { - dist[edge.getEnd()] = poll.getWeight() + edge.getWeight(); - queue.add(new Edge(edge.getEnd(), dist[edge.getEnd()])); - } - } - } - } - - static class Edge { - - private int end; - private int weight; - - public Edge(int end, int weight) { - this.end = end; - this.weight = weight; - } - - public int getEnd() { - return end; - } - - public int getWeight() { - return weight; - } - } -} diff --git a/eonju/BOJ/BOJ_16234.java b/eonju/BOJ/BOJ_16234.java deleted file mode 100644 index 66880c6..0000000 --- a/eonju/BOJ/BOJ_16234.java +++ /dev/null @@ -1,115 +0,0 @@ -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.util.LinkedList; -import java.util.Queue; - -class BOJ_16234 { - - private static int[] moveI = {-1, 1, 0, 0}; - private static int[] moveJ = {0, 0, -1, 1}; - private static int[][] map; - private static int[][] copyMap; - private static boolean[][] visited; - private static boolean flag; - private static int N; - private static int L; - private static int R; - - public static void main(String[] args) throws IOException { - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); - String[] input = bufferedReader.readLine().split(" "); - N = Integer.parseInt(input[0]); - L = Integer.parseInt(input[1]); - R = Integer.parseInt(input[2]); - - map = new int[N][N]; - visited = new boolean[N][N]; - - for (int i = 0; i < N; i++) { - input = bufferedReader.readLine().split(" "); - for (int j = 0; j < N; j++) { - map[i][j] = Integer.parseInt(input[j]); - } - } - - int year = 0; - while (true) { - flag = false; - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - if (!visited[i][j]) { - copyMap = new int[N][N]; - visited = new boolean[N][N]; - bfs(i, j); - } - - } - } - - if (flag) { - year++; - } else { - break; - } - } - - System.out.println(year); - - } - - public static void bfs(int startI, int startJ) { - Queue queue = new LinkedList<>(); - visited[startI][startJ] = true; - queue.add(new int[]{startI, startJ}); - - int sum = map[startI][startJ]; - int cnt = 1; - - while (!queue.isEmpty()) { - int[] poll = queue.poll(); - int nowI = poll[0]; - int nowJ = poll[1]; - - for (int i = 0; i < 4; i++) { - int nextI = nowI + moveI[i]; - int nextJ = nowJ + moveJ[i]; - - if (nextI < 0 || nextJ < 0 || nextI >= N || nextJ >= N) { - continue; - } - - if (visited[nextI][nextJ]) { - continue; - } - - if (isInPopulation(map[nowI][nowJ] - map[nextI][nextJ])) { - queue.add(new int[]{nextI, nextJ}); - - copyMap[nextI][nextJ] = map[nextI][nextJ]; - sum += map[nextI][nextJ]; - cnt++; - flag = true; - } - } - } - - movePopulation(sum, cnt); - } - - public static void movePopulation(int sum, int cnt) { - int people = sum / cnt; - - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - if (copyMap[i][j] > 0) { - map[i][j] = people; - } - } - } - } - - public static boolean isInPopulation(int number) { - return number >= L && number <= R; - } -} From 59a24a76a8edd5a0f8bfe94be631ac20f91b2d01 Mon Sep 17 00:00:00 2001 From: GetMapping Date: Mon, 7 Mar 2022 08:33:21 +0900 Subject: [PATCH 3/5] unsolve: BOJ_16234 --- eonju/BOJ/BOJ_16234.java | 115 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 eonju/BOJ/BOJ_16234.java diff --git a/eonju/BOJ/BOJ_16234.java b/eonju/BOJ/BOJ_16234.java new file mode 100644 index 0000000..66880c6 --- /dev/null +++ b/eonju/BOJ/BOJ_16234.java @@ -0,0 +1,115 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.LinkedList; +import java.util.Queue; + +class BOJ_16234 { + + private static int[] moveI = {-1, 1, 0, 0}; + private static int[] moveJ = {0, 0, -1, 1}; + private static int[][] map; + private static int[][] copyMap; + private static boolean[][] visited; + private static boolean flag; + private static int N; + private static int L; + private static int R; + + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String[] input = bufferedReader.readLine().split(" "); + N = Integer.parseInt(input[0]); + L = Integer.parseInt(input[1]); + R = Integer.parseInt(input[2]); + + map = new int[N][N]; + visited = new boolean[N][N]; + + for (int i = 0; i < N; i++) { + input = bufferedReader.readLine().split(" "); + for (int j = 0; j < N; j++) { + map[i][j] = Integer.parseInt(input[j]); + } + } + + int year = 0; + while (true) { + flag = false; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (!visited[i][j]) { + copyMap = new int[N][N]; + visited = new boolean[N][N]; + bfs(i, j); + } + + } + } + + if (flag) { + year++; + } else { + break; + } + } + + System.out.println(year); + + } + + public static void bfs(int startI, int startJ) { + Queue queue = new LinkedList<>(); + visited[startI][startJ] = true; + queue.add(new int[]{startI, startJ}); + + int sum = map[startI][startJ]; + int cnt = 1; + + while (!queue.isEmpty()) { + int[] poll = queue.poll(); + int nowI = poll[0]; + int nowJ = poll[1]; + + for (int i = 0; i < 4; i++) { + int nextI = nowI + moveI[i]; + int nextJ = nowJ + moveJ[i]; + + if (nextI < 0 || nextJ < 0 || nextI >= N || nextJ >= N) { + continue; + } + + if (visited[nextI][nextJ]) { + continue; + } + + if (isInPopulation(map[nowI][nowJ] - map[nextI][nextJ])) { + queue.add(new int[]{nextI, nextJ}); + + copyMap[nextI][nextJ] = map[nextI][nextJ]; + sum += map[nextI][nextJ]; + cnt++; + flag = true; + } + } + } + + movePopulation(sum, cnt); + } + + public static void movePopulation(int sum, int cnt) { + int people = sum / cnt; + + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + if (copyMap[i][j] > 0) { + map[i][j] = people; + } + } + } + } + + public static boolean isInPopulation(int number) { + return number >= L && number <= R; + } +} From 22a17c33b77561484d0b0ca69ba2eacd378cbbee Mon Sep 17 00:00:00 2001 From: GetMapping Date: Mon, 7 Mar 2022 08:34:36 +0900 Subject: [PATCH 4/5] solve: BOJ_1238 --- eonju/BOJ/BOJ_1238.java | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 eonju/BOJ/BOJ_1238.java diff --git a/eonju/BOJ/BOJ_1238.java b/eonju/BOJ/BOJ_1238.java new file mode 100644 index 0000000..62dff90 --- /dev/null +++ b/eonju/BOJ/BOJ_1238.java @@ -0,0 +1,103 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.PriorityQueue; + +class BOJ_1238 { + + private static int N; + private static int M; + private static int X; + private static HashMap> graph; + private static int[] dist; + + public static void main(String[] args) throws IOException { + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); + String[] input = bufferedReader.readLine().split(" "); + N = Integer.parseInt(input[0]); + M = Integer.parseInt(input[1]); + X = Integer.parseInt(input[2]); + + graph = new HashMap<>(); + for (int i = 1; i <= N; i++) { + graph.put(i, new ArrayList<>()); + } + + for (int i = 0; i < M; i++) { + input = bufferedReader.readLine().split(" "); + int targetA = Integer.parseInt(input[0]); + int targetB = Integer.parseInt(input[1]); + int weight = Integer.parseInt(input[2]); + + graph.get(targetA).add(new Edge(targetB, weight)); + } + + dist = new int[N + 1]; + Arrays.fill(dist, Integer.MAX_VALUE); + + dijkstra(X); + int[] answers = dist.clone(); + + for (int i = 1; i <= N; i++) { + Arrays.fill(dist, Integer.MAX_VALUE); + dijkstra(i); + answers[i] += dist[X]; + } + + int max = answers[1]; + for (int i = 1; i <= N; i++) { + if (max < answers[i]) { + max = answers[i]; + } + } + + System.out.println(max); + } + + public static void dijkstra(int start) { + PriorityQueue queue = new PriorityQueue<>(Comparator.comparingInt(Edge::getWeight)); + + dist[start] = 0; + queue.add(new Edge(start, 0)); + + while (!queue.isEmpty()) { + Edge poll = queue.poll(); + + if (poll.getWeight() > dist[poll.getEnd()]) { + continue; + } + + List edges = graph.get(poll.getEnd()); + for (Edge edge : edges) { + if (poll.getWeight() + edge.getWeight() < dist[edge.getEnd()]) { + dist[edge.getEnd()] = poll.getWeight() + edge.getWeight(); + queue.add(new Edge(edge.getEnd(), dist[edge.getEnd()])); + } + } + } + } + + static class Edge { + + private int end; + private int weight; + + public Edge(int end, int weight) { + this.end = end; + this.weight = weight; + } + + public int getEnd() { + return end; + } + + public int getWeight() { + return weight; + } + } +} From eff0e75f2dfa15f171bbc227e41e94c220298169 Mon Sep 17 00:00:00 2001 From: GetMapping Date: Mon, 7 Mar 2022 09:07:32 +0900 Subject: [PATCH 5/5] =?UTF-8?q?unsolve:=20BOJ=5F16234=20=EC=8B=9C=EA=B0=84?= =?UTF-8?q?=EC=B4=88=EA=B3=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- eonju/BOJ/BOJ_16234.java | 68 +++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 35 deletions(-) diff --git a/eonju/BOJ/BOJ_16234.java b/eonju/BOJ/BOJ_16234.java index 66880c6..6dceef7 100644 --- a/eonju/BOJ/BOJ_16234.java +++ b/eonju/BOJ/BOJ_16234.java @@ -1,7 +1,10 @@ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedList; +import java.util.List; import java.util.Queue; class BOJ_16234 { @@ -9,9 +12,9 @@ class BOJ_16234 { private static int[] moveI = {-1, 1, 0, 0}; private static int[] moveJ = {0, 0, -1, 1}; private static int[][] map; - private static int[][] copyMap; private static boolean[][] visited; - private static boolean flag; + private static List moveList; + private static boolean isMove; private static int N; private static int L; private static int R; @@ -24,52 +27,50 @@ public static void main(String[] args) throws IOException { R = Integer.parseInt(input[2]); map = new int[N][N]; - visited = new boolean[N][N]; + moveList = new ArrayList<>(); for (int i = 0; i < N; i++) { - input = bufferedReader.readLine().split(" "); - for (int j = 0; j < N; j++) { - map[i][j] = Integer.parseInt(input[j]); - } + map[i] = Arrays.stream(bufferedReader.readLine().split(" ")) + .mapToInt(Integer::parseInt) + .toArray(); } - int year = 0; + int day = 0; while (true) { - flag = false; + isMove = false; + visited = new boolean[N][N]; + moveList = new ArrayList<>(); + for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { if (!visited[i][j]) { - copyMap = new int[N][N]; - visited = new boolean[N][N]; bfs(i, j); } - } } - if (flag) { - year++; + if (isMove) { + day++; } else { break; } } - - System.out.println(year); - + System.out.println(day); } public static void bfs(int startI, int startJ) { - Queue queue = new LinkedList<>(); visited[startI][startJ] = true; + + Queue queue = new LinkedList<>(); queue.add(new int[]{startI, startJ}); - int sum = map[startI][startJ]; - int cnt = 1; + int sum = 0; while (!queue.isEmpty()) { int[] poll = queue.poll(); int nowI = poll[0]; int nowJ = poll[1]; + sum += map[nowI][nowJ]; for (int i = 0; i < 4; i++) { int nextI = nowI + moveI[i]; @@ -83,29 +84,26 @@ public static void bfs(int startI, int startJ) { continue; } - if (isInPopulation(map[nowI][nowJ] - map[nextI][nextJ])) { + int diff = Math.abs(map[nowI][nowJ] - map[nextI][nextJ]); + if (isInPopulation(diff)) { + visited[nextI][nextJ] = true; queue.add(new int[]{nextI, nextJ}); - - copyMap[nextI][nextJ] = map[nextI][nextJ]; - sum += map[nextI][nextJ]; - cnt++; - flag = true; + moveList.add(new int[]{nextI, nextJ}); + isMove = true; } } } - movePopulation(sum, cnt); + if (isMove) { + movePopulation(sum); + } } - public static void movePopulation(int sum, int cnt) { - int people = sum / cnt; + public static void movePopulation(int sum) { + int avg = sum / moveList.size(); - for (int i = 0; i < N; i++) { - for (int j = 0; j < N; j++) { - if (copyMap[i][j] > 0) { - map[i][j] = people; - } - } + for (int[] location : moveList) { + map[location[0]][location[1]] = avg; } }