Repository: Study-Till-Death/algorithm-study
Pull request #93: week16
Author: login: realain
Created: 2025-04-11T07:52:27Z | Merged: 2025-04-25T00:19:06Z

![KakaoTalk_20250309_135910067](https://github.com/user-attachments/assets/77b6c794-3639-4d56-bc25-eaad5efeec7e)



Diff:
diff --git a/src/main/java/com/example/algorithmstudy/week16/MyAnswer046.java b/src/main/java/com/example/algorithmstudy/week16/MyAnswer046.java
new file mode 100644
index 0000000..a4317c3
--- /dev/null
+++ b/src/main/java/com/example/algorithmstudy/week16/MyAnswer046.java
@@ -0,0 +1,113 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer046 {
+
+    static class Point {
+        int x, y, time;
+        Point(int x, int y, int time) {
+            this.x = x;
+            this.y = y;
+            this.time = time;
+        }
+    }
+
+    int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상하좌우 이동
+
+    private int[][] bfs(int[][] board, int startX, int startY) {
+        int n = board.length;
+        int m = board[0].length;
+        int[][] time = new int[n][m];
+        for (int i = 0; i < n; i++) Arrays.fill(time[i], -1);
+
+        Queue<Point> queue = new LinkedList<>();
+        queue.offer(new Point(startX, startY, 0));
+        time[startX][startY] = 0;
+
+        while (!queue.isEmpty()) {
+            Point now = queue.poll();
+
+            for (int[] move : directions) {
+                int nx = now.x + move[0];
+                int ny = now.y + move[1];
+
+                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
+                    if (board[nx][ny] != 1 && time[nx][ny] == -1) {
+                        time[nx][ny] = now.time + 1;
+                        queue.offer(new Point(nx, ny, now.time + 1));
+                    }
+                }
+            }
+        }
+
+        return time;
+    }
+
+    public int solution(int[][] board){
+        int n = board.length;
+        int m = board[0].length;
+
+        int start2X = -1, start2Y = -1;
+        int start3X = -1, start3Y = -1;
+        List<int[]> berryLocations = new ArrayList<>();
+
+        for (int i = 0; i < n; i++) {
+            for (int j = 0; j < m; j++) {
+                if (board[i][j] == 2) {
+                    start2X = i;
+                    start2Y = j;
+                } else if (board[i][j] == 3) {
+                    start3X = i;
+                    start3Y = j;
+                } else if (board[i][j] == 4) {
+                    berryLocations.add(new int[]{i, j});
+                }
+            }
+        }
+
+        int[][] timeFrom2to4 = bfs(board, start2X, start2Y);
+        int[][] timeFrom3to4 = bfs(board, start3X, start3Y);
+
+        int minTime = Integer.MAX_VALUE;
+
+        for (int[] location : berryLocations) {
+            int fx = location[0];
+            int fy = location[1];
+
+            int t1 = timeFrom2to4[fx][fy];
+            int t2 = timeFrom3to4[fx][fy];
+
+            if (t1 != -1 && t2 != -1) {
+                int totalTime = t1 + t2;
+                if (totalTime < minTime) {
+                    minTime = totalTime;
+                }
+            }
+        }
+
+        return (minTime == Integer.MAX_VALUE) ? -1 : minTime;
+    }
+
+    public static void main(String[] args){
+        MyAnswer046 T = new MyAnswer046();
+        System.out.println(T.solution(new int[][]{
+                {4, 1, 0, 0, 0, 0, 1, 0},
+                {0, 0, 0, 1, 0, 1, 0, 0},
+                {0, 2, 1, 1, 3, 0, 4, 0},
+                {0, 0, 0, 4, 1, 1, 1, 0}}));
+        System.out.println(T.solution(new int[][]{
+                {3, 0, 0, 0, 1, 4, 4, 4},
+                {0, 1, 1, 0, 0, 0, 1, 0},
+                {0, 1, 4, 0, 1, 0, 0, 0},
+                {0, 0, 0, 1, 0, 0, 0, 0},
+                {1, 0, 1, 0, 0, 1, 1, 0},
+                {4, 0, 0, 0, 1, 0, 0, 0},
+                {4, 1, 0, 0, 1, 0, 0, 0},
+                {4, 0, 0, 0, 0, 0, 1, 2}}));
+        System.out.println(T.solution(new int[][]{
+                {4, 1, 0, 1, 0},
+                {0, 1, 0, 1, 0},
+                {0, 0, 2, 3, 4},
+                {0, 1, 0, 1, 0}}));
+    }
+}
diff --git a/src/main/java/com/example/algorithmstudy/week16/MyAnswer047.java b/src/main/java/com/example/algorithmstudy/week16/MyAnswer047.java
new file mode 100644
index 0000000..27386fa
--- /dev/null
+++ b/src/main/java/com/example/algorithmstudy/week16/MyAnswer047.java
@@ -0,0 +1,42 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer047 {
+    public int solution(int n, int[][] flights, int s, int e, int k) {
+        List<List<int[]>> graph = new ArrayList<>();
+        for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
+        for (int[] flight : flights) {
+            graph.get(flight[0]).add(new int[]{flight[1], flight[2]});
+        }
+
+        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
+        pq.offer(new int[]{s, 0, -1});
+
+        while (!pq.isEmpty()) {
+            int[] current = pq.poll();
+            int city = current[0];
+            int cost = current[1];
+            int transfers = current[2];
+
+            if (city == e) return cost;
+            if (transfers >= k) continue;
+
+            for (int[] next : graph.get(city)) {
+                int nextCity = next[0];
+                int nextCost = next[1];
+                pq.offer(new int[]{nextCity, cost + nextCost, transfers + 1});
+            }
+        }
+
+        return -1;
+    }
+
+    public static void main(String[] args){
+        MyAnswer047 T = new MyAnswer047();
+        System.out.println(T.solution(5, new int[][]{{0, 1, 10}, {1, 2, 20}, {0, 2, 70}, {0, 3, 100}, {1, 3, 80}, {2, 3, 10}, {2, 4, 30}, {3, 4, 10}}, 0, 3, 1));
+        System.out.println(T.solution(4, new int[][]{{0, 1, 10}, {0, 2, 10}, {1, 3, 5}, {2, 3, 3}}, 0, 3, 0));
+        System.out.println(T.solution(8, new int[][]{{0, 3, 10}, {1, 5, 10}, {1, 7, 100}, {0, 1, 10}, {0, 2, 10}, {5, 7, 30}, {3, 7, 10}, {1, 3, 5}, {2, 3, 3}}, 1, 7, 2));
+        System.out.println(T.solution(10, new int[][]{{1, 8, 50}, {0, 8, 30}, {1, 0, 10}, {2, 8, 10}, {0, 3, 10}, {1, 5, 10}, {1, 7, 100}, {0, 1, 10}, {0, 2, 10}, {5, 7, 30}, {3, 7, 10}, {1, 3, 5}, {2, 3, 3}}, 1, 8, 2));
+        System.out.println(T.solution(4, new int[][]{{0, 3, 59},{2, 0, 83}, {3, 1, 16}, {1, 3, 16}}, 3, 0, 3));
+    }
+}
diff --git a/src/main/java/com/example/algorithmstudy/week16/MyAnswer048.java b/src/main/java/com/example/algorithmstudy/week16/MyAnswer048.java
new file mode 100644
index 0000000..e1bdbea
--- /dev/null
+++ b/src/main/java/com/example/algorithmstudy/week16/MyAnswer048.java
@@ -0,0 +1,56 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer048 {
+    public int solution(int[][] routes, int s, int e) {
+        if (s == e) return 0;
+
+        Map<Integer, Set<Integer>> graph = new HashMap<>();
+        int n = routes.length;
+
+        for (int i = 0; i < n; i++) {
+            for (int stop : routes[i]) {
+                graph.putIfAbsent(stop, new HashSet<>());
+                graph.get(stop).add(i);
+            }
+        }
+
+        Queue<Integer> Q = new LinkedList<>();
+        Set<Integer> visitedStops = new HashSet<>();
+        boolean[] visitedLines = new boolean[n];
+
+        Q.offer(s);
+        visitedStops.add(s);
+
+        int transfer = 0;
+
+        while (!Q.isEmpty()) {
+            int size = Q.size();
+            for (int i = 0; i < size; i++) {
+                int curStop = Q.poll();
+                for (int line : graph.getOrDefault(curStop, new HashSet<>())) {
+                    if (visitedLines[line]) continue;
+                    visitedLines[line] = true;
+                    for (int nextStop : routes[line]) {
+                        if (nextStop == e) return transfer;
+                        if (!visitedStops.contains(nextStop)) {
+                            visitedStops.add(nextStop);
+                            Q.offer(nextStop);
+                        }
+                    }
+                }
+            }
+            transfer++;
+        }
+
+        return -1;
+    }
+
+    public static void main(String[] args){
+        MyAnswer048 T = new MyAnswer048();
+        System.out.println(T.solution(new int[][]{{1, 2, 3, 4, 5, 6, 19}, {2, 7, 8, 13}, {5, 9, 10}, {9, 11, 12, 18}, {13, 14, 15}, {14, 12, 16, 17}}, 1, 12));
+        System.out.println(T.solution(new int[][]{{1, 3, 5, 7}, {9, 3, 12}, {6, 5, 8}, {2, 8, 14, 15}, {2, 14, 16}}, 1, 14));
+        System.out.println(T.solution(new int[][]{{7, 12},{5, 19},{7, 19},{9, 12, 13},{9, 5, 15}}, 9, 19));
+        System.out.println(T.solution(new int[][]{{1, 2, 3, 4, 5},{9, 7, 10},{7, 6, 3, 8}, {5, 11, 8, 12}}, 1, 10));
+    }
+}


Reviews:
user: login: anjungin
body: ^^
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-04-22T23:18:41Z

Review comments:
diff_hunk: @@ -0,0 +1,113 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer046 {
+
+    static class Point {
+        int x, y, time;
+        Point(int x, int y, int time) {
+            this.x = x;
+            this.y = y;
+            this.time = time;
+        }
+    }
+
+    int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상하좌우 이동
+
+    private int[][] bfs(int[][] board, int startX, int startY) {
+        int n = board.length;
+        int m = board[0].length;
+        int[][] time = new int[n][m];
+        for (int i = 0; i < n; i++) Arrays.fill(time[i], -1);
+
+        Queue<Point> queue = new LinkedList<>();
+        queue.offer(new Point(startX, startY, 0));
+        time[startX][startY] = 0;
+
+        while (!queue.isEmpty()) {
+            Point now = queue.poll();
+
+            for (int[] move : directions) {
+                int nx = now.x + move[0];
+                int ny = now.y + move[1];
+
+                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
+                    if (board[nx][ny] != 1 && time[nx][ny] == -1) {
+                        time[nx][ny] = now.time + 1;
+                        queue.offer(new Point(nx, ny, now.time + 1));
+                    }
+                }
+            }
+        }
+
+        return time;
+    }
path: src/main/java/com/example/algorithmstudy/week16/MyAnswer046.java
user: login: anjungin
body: 초심찾는 문제였네

created_at: 2025-04-22T23:12:26Z
author_association: COLLABORATOR
diff_hunk: @@ -0,0 +1,113 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer046 {
+
+    static class Point {
+        int x, y, time;
+        Point(int x, int y, int time) {
+            this.x = x;
+            this.y = y;
+            this.time = time;
+        }
+    }
+
+    int[][] directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 상하좌우 이동
+
+    private int[][] bfs(int[][] board, int startX, int startY) {
+        int n = board.length;
+        int m = board[0].length;
+        int[][] time = new int[n][m];
+        for (int i = 0; i < n; i++) Arrays.fill(time[i], -1);
+
+        Queue<Point> queue = new LinkedList<>();
+        queue.offer(new Point(startX, startY, 0));
+        time[startX][startY] = 0;
+
+        while (!queue.isEmpty()) {
+            Point now = queue.poll();
+
+            for (int[] move : directions) {
+                int nx = now.x + move[0];
+                int ny = now.y + move[1];
+
+                if (nx >= 0 && nx < n && ny >= 0 && ny < m) {
+                    if (board[nx][ny] != 1 && time[nx][ny] == -1) {
+                        time[nx][ny] = now.time + 1;
+                        queue.offer(new Point(nx, ny, now.time + 1));
+                    }
+                }
+            }
+        }
+
+        return time;
+    }
+
+    public int solution(int[][] board){
+        int n = board.length;
+        int m = board[0].length;
+
+        int start2X = -1, start2Y = -1;
+        int start3X = -1, start3Y = -1;
+        List<int[]> berryLocations = new ArrayList<>();
+
+        for (int i = 0; i < n; i++) {
+            for (int j = 0; j < m; j++) {
+                if (board[i][j] == 2) {
+                    start2X = i;
+                    start2Y = j;
+                } else if (board[i][j] == 3) {
+                    start3X = i;
+                    start3Y = j;
+                } else if (board[i][j] == 4) {
+                    berryLocations.add(new int[]{i, j});
+                }
+            }
+        }
path: src/main/java/com/example/algorithmstudy/week16/MyAnswer046.java
user: login: anjungin
body: 열심히 푸는 모습 보기 좋아요
created_at: 2025-04-22T23:17:16Z
author_association: COLLABORATOR
diff_hunk: @@ -0,0 +1,42 @@
+package com.example.algorithmstudy.week16;
+import java.util.*;
+
+public class MyAnswer047 {
+    public int solution(int n, int[][] flights, int s, int e, int k) {
+        List<List<int[]>> graph = new ArrayList<>();
+        for (int i = 0; i < n; i++) graph.add(new ArrayList<>());
+        for (int[] flight : flights) {
+            graph.get(flight[0]).add(new int[]{flight[1], flight[2]});
+        }
+
+        PriorityQueue<int[]> pq = new PriorityQueue<>(Comparator.comparingInt(a -> a[1]));
+        pq.offer(new int[]{s, 0, -1});
+
+        while (!pq.isEmpty()) {
+            int[] current = pq.poll();
+            int city = current[0];
+            int cost = current[1];
+            int transfers = current[2];
+
+            if (city == e) return cost;
+            if (transfers >= k) continue;
+
+            for (int[] next : graph.get(city)) {
+                int nextCity = next[0];
+                int nextCost = next[1];
+                pq.offer(new int[]{nextCity, cost + nextCost, transfers + 1});
+            }
+        }
path: src/main/java/com/example/algorithmstudy/week16/MyAnswer047.java
user: login: anjungin
body: 갈금하시네요 잘봤어요
created_at: 2025-04-22T23:18:20Z
author_association: COLLABORATOR

PR comments:
user: login: github-actions[bot]
created_at: 2025-04-11T07:52:43Z
body: @anjungin 님, @protectHeo 님 숭고한 랜덤 리뷰어로 당첨되셨습니다! 리뷰를 부탁드립니다. 🙏
author_association: NONE
user: login: protectHeo
created_at: 2025-04-22T23:19:08Z
body: 눈물이 없는 사람인데 DFS혐오자가 BFS 곧잘 쓰는거보니 눈물이 나요
author_association: COLLABORATOR