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..6dceef7 --- /dev/null +++ b/eonju/BOJ/BOJ_16234.java @@ -0,0 +1,113 @@ +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 { + + private static int[] moveI = {-1, 1, 0, 0}; + private static int[] moveJ = {0, 0, -1, 1}; + private static int[][] map; + private static boolean[][] visited; + private static List moveList; + private static boolean isMove; + 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]; + moveList = new ArrayList<>(); + + for (int i = 0; i < N; i++) { + map[i] = Arrays.stream(bufferedReader.readLine().split(" ")) + .mapToInt(Integer::parseInt) + .toArray(); + } + + int day = 0; + while (true) { + 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]) { + bfs(i, j); + } + } + } + + if (isMove) { + day++; + } else { + break; + } + } + System.out.println(day); + } + + public static void bfs(int startI, int startJ) { + visited[startI][startJ] = true; + + Queue queue = new LinkedList<>(); + queue.add(new int[]{startI, startJ}); + + 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]; + int nextJ = nowJ + moveJ[i]; + + if (nextI < 0 || nextJ < 0 || nextI >= N || nextJ >= N) { + continue; + } + + if (visited[nextI][nextJ]) { + continue; + } + + int diff = Math.abs(map[nowI][nowJ] - map[nextI][nextJ]); + if (isInPopulation(diff)) { + visited[nextI][nextJ] = true; + queue.add(new int[]{nextI, nextJ}); + moveList.add(new int[]{nextI, nextJ}); + isMove = true; + } + } + } + + if (isMove) { + movePopulation(sum); + } + } + + public static void movePopulation(int sum) { + int avg = sum / moveList.size(); + + for (int[] location : moveList) { + map[location[0]][location[1]] = avg; + } + } + + public static boolean isInPopulation(int number) { + return number >= L && number <= R; + } +}