From 7e5d8e1ff0b0675016740d7cbfc5cdda4ea4cf9e Mon Sep 17 00:00:00 2001 From: dk7648 Date: Mon, 25 Nov 2024 00:26:37 +0900 Subject: [PATCH 1/4] =?UTF-8?q?complete=20[W5]=20=EB=B2=BD=20=EB=B6=80?= =?UTF-8?q?=EC=88=98=EA=B3=A0=20=EC=9D=B4=EB=8F=99=ED=95=98=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week5/KBC/boj_2206.java | 103 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 week5/KBC/boj_2206.java diff --git a/week5/KBC/boj_2206.java b/week5/KBC/boj_2206.java new file mode 100644 index 0000000..e3259af --- /dev/null +++ b/week5/KBC/boj_2206.java @@ -0,0 +1,103 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static final int MAX = 1000000; + static int[][] map; + static int height; + static int width; + + static int[] dx = {1, 0, -1, 0}; + static int[] dy = {0, 1, 0, -1}; + + static class Pos { + int x; + int y; + + public Pos(int x, int y) { + this.x = x; + this.y = y; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] input = br.readLine().split(" "); + height = Integer.parseInt(input[0]); + width = Integer.parseInt(input[1]); + map = new int[height][width]; + for (int h = 0; h < height; h++) { + int[] numbers = Arrays.stream(br.readLine().split("")).mapToInt(Integer::parseInt).toArray(); + for (int w = 0; w < width; w++) { + map[h][w] = numbers[w]; + } + } + + int[][] distanceMapToStart = bfs(0, 0); + int[][] distanceMapToEnd = bfs(width - 1, height - 1); + int minDistance = MAX; + if(distanceMapToStart[height-1][width-1] != 0) { + minDistance = distanceMapToStart[height-1][width-1]; + } + for (int h = 0; h < height; h++) { + for (int w = 0; w < width; w++) { + if (map[h][w] == 1) { + int min_start = MAX; + int min_end = MAX; + for (int i = 0; i < 4; i++) { + int newX = w + dx[i]; + int newY = h + dy[i]; + if (newX < 0 || newX >= width || newY < 0 || newY >= height || map[newY][newX] == 1) { + continue; + } + int distanceStart = distanceMapToStart[h + dy[i]][w + dx[i]]; + int distanceEnd = distanceMapToEnd[h + dy[i]][w + dx[i]]; + if (distanceStart != 0) { + min_start = Math.min(distanceStart, min_start); + } + if (distanceEnd != 0) { + min_end = Math.min(distanceEnd, min_end); + } + } + minDistance = Math.min(minDistance, min_start + min_end + 1); + } + } + } + if (minDistance == MAX) { + System.out.println(-1); + return; + } + System.out.println(minDistance); + } + + public static int[][] bfs(int startX, int startY) { + int[][] distanceMap = new int[height][width]; + int distance = 1; + Queue queue = new LinkedList<>(); + if (map[startY][startX] != 1) { + queue.add(new Pos(startX, startY)); + distanceMap[startY][startX] = distance; + distance++; + } + + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Pos curPosition = queue.poll(); + for (int direct = 0; direct < 4; direct++) { + int newX = curPosition.x + dx[direct]; + int newY = curPosition.y + dy[direct]; + if (newX < 0 || newX >= width || newY < 0 || newY >= height || distanceMap[newY][newX] != 0 || map[newY][newX] == 1) { + continue; + } + distanceMap[newY][newX] = distance; + queue.add(new Pos(newX, newY)); + } + } + distance++; + } + return distanceMap; + } +} From 1e57b3bceb56b09f714bb38716fb187468a79f11 Mon Sep 17 00:00:00 2001 From: dk7648 Date: Mon, 25 Nov 2024 00:26:52 +0900 Subject: [PATCH 2/4] =?UTF-8?q?complete=20[W5]=20=EB=8B=AC=EC=9D=B4=20?= =?UTF-8?q?=EC=B0=A8=EC=98=A4=EB=A5=B8=EB=8B=A4,=20=EA=B0=80=EC=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week5/KBC/boj_1194.java | 118 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 week5/KBC/boj_1194.java diff --git a/week5/KBC/boj_1194.java b/week5/KBC/boj_1194.java new file mode 100644 index 0000000..a69dfab --- /dev/null +++ b/week5/KBC/boj_1194.java @@ -0,0 +1,118 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.*; + +public class Main { + static class Data { + int y; + int x; + int count; + int keys; + int prevX; + int prevY; + + public Data(int y, int x, int count) { + this.y = y; + this.x = x; + this.count = count; + keys = 0b0; + } + + public Data(Data data) { + this.y = data.y; + this.x = data.x; + this.count = data.count; + this.keys = data.keys; + this.prevX = data.prevX; + this.prevY = data.prevY; + } + + public void pickKey(int key) { + keys = keys | key; + } + + public boolean isPickedKey(int key) { + return (keys & key) > 0; + } + } + + static boolean[][][] visit; + static final int[] dy = {0, -1, 0, 1}; + static final int[] dx = {-1, 0, 1, 0}; + static String[][] map; + static int height; + static int width; + + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + String[] input = br.readLine().split(" "); + height = Integer.parseInt(input[0]); + width = Integer.parseInt(input[1]); + map = new String[height][width]; + visit = new boolean[64][height][width]; + Data start = null; + for (int y = 0; y < height; y++) { + input = br.readLine().split(""); + for (int x = 0; x < width; x++) { + map[y][x] = input[x]; + if (map[y][x].equals("0")) { + start = new Data(y, x, 0); + map[y][x] = "."; + } + } + } + + if (start != null) { + System.out.println(bfs(start)); + return; + } + System.out.println("시작 위치가 없습니다."); + } + + public static int bfs(Data start) { + Queue queue = new LinkedList<>(); + queue.add(start); + + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Data data = queue.poll(); + for (int k = 0; k < 4; k++) { + Data newData = new Data(data); + newData.y += dy[k]; + newData.x += dx[k]; + newData.count += 1; + + if (outOfRange(newData)) { + continue; + } + String curTile = map[newData.y][newData.x]; + if (visit[newData.keys][newData.y][newData.x] || curTile.equals("#")) { + continue; + } + visit[newData.keys][newData.y][newData.x] = true; + if (curTile.equals("1")) { + return newData.count; + } + if (curTile.matches("^[a-f]$")) { //열쇠 + newData.pickKey(0b1 << (curTile.charAt(0) - 'a')); + queue.add(newData); + } else if (curTile.matches("^[A-F]$")) { //문 + if (newData.isPickedKey(0b1 << (curTile.charAt(0) - 'A'))) { + queue.add(newData); + } + } else if (curTile.equals(".")) { + queue.add(newData); + } + } + } + } + return -1; + } + + private static boolean outOfRange(Data newData) { + return newData.y < 0 || newData.y >= map.length || newData.x < 0 || newData.x >= map[0].length; + } +} From 480e1083da0e90f44bdf4f8df01883f6eaa4115a Mon Sep 17 00:00:00 2001 From: dk7648 Date: Mon, 25 Nov 2024 00:27:05 +0900 Subject: [PATCH 3/4] =?UTF-8?q?complete=20[W5]=20=EC=95=84=EA=B8=B0=20?= =?UTF-8?q?=EC=83=81=EC=96=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week5/KBC/boj_16236.java | 109 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 week5/KBC/boj_16236.java diff --git a/week5/KBC/boj_16236.java b/week5/KBC/boj_16236.java new file mode 100644 index 0000000..000f0a6 --- /dev/null +++ b/week5/KBC/boj_16236.java @@ -0,0 +1,109 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; + +public class Main { + static class Shark { + int y; + int x; + int size; + int time; + int ate; + + public Shark(int y, int x, int size, int time, int ate) { + this.y = y; + this.x = x; + this.time = time; + if (ate >= size) { + ate = 0; + size++; + } + this.size = size; + this.ate = ate; + } + } + + static int n; + static int[][] map; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + n = Integer.parseInt(br.readLine()); + + Shark babyShark = null; + map = new int[n][n]; + for (int i = 0; i < n; i++) { + int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + for (int k = 0; k < n; k++) { + map[i][k] = input[k]; + if (map[i][k] == 9) { + babyShark = new Shark(i, k, 2, 0, 0); + map[i][k] = 0; + } + } + } + + if (babyShark == null) { + return; + } + + int result = bfs(babyShark); + System.out.println(result); + } + + public static int bfs(Shark babyShark) { + int[] dx = {0, -1, 1, 0}; + int[] dy = {-1, 0, 0, 1}; + Queue queue = new LinkedList<>(); + Queue canEat = new LinkedList<>(); + queue.add(babyShark); + boolean[][] isVisit = new boolean[n][n]; + int aliveTime = 0; + while (!queue.isEmpty()) { + int size = queue.size(); + for (int i = 0; i < size; i++) { + Shark current = queue.poll(); + isVisit[current.y][current.x] = true; + for (int k = 0; k < 4; k++) { + int newY = current.y + dy[k]; + int newX = current.x + dx[k]; + if (isOutOfRange(newY, newX) || isVisit[newY][newX] || current.size < map[newY][newX]) { //지나갈수 없을 때 + continue; + } + if (map[newY][newX] == 0 || map[newY][newX] == current.size) { //지나갈 수 있을 때 + isVisit[newY][newX] = true; + queue.add(new Shark(newY, newX, current.size, current.time + 1, current.ate)); + } else if (current.size > map[newY][newX]) { // 먹을 수 있을 때 + isVisit[newY][newX] = true; + canEat.add(new Shark(newY, newX, current.size, current.time + 1, current.ate + 1)); + } + + } + } + if (!canEat.isEmpty()) { + Shark target = canEat.poll(); + while (!canEat.isEmpty()) { + Shark temp = canEat.poll(); + if (temp.y < target.y) { + target = temp; + } else if (temp.y == target.y && temp.x < target.x) { + target = temp; + } + } + isVisit = new boolean[n][n]; + map[target.y][target.x] = 0; + aliveTime = target.time; + queue.clear(); + queue.add(target); + } + } + return aliveTime; + } + + private static boolean isOutOfRange(int newY, int newX) { + return newY < 0 || newY >= n || newX < 0 || newX >= n; + } +} From e38b12f4c9fdd21609fbbdd78b46c0b47ce49ccc Mon Sep 17 00:00:00 2001 From: dk7648 Date: Mon, 25 Nov 2024 00:27:28 +0900 Subject: [PATCH 4/4] =?UTF-8?q?complete=20[W5]=20=EC=8B=9C=ED=97=98=20?= =?UTF-8?q?=EA=B0=90=EB=8F=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- week5/KBC/boj_13458.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 week5/KBC/boj_13458.java diff --git a/week5/KBC/boj_13458.java b/week5/KBC/boj_13458.java new file mode 100644 index 0000000..33f7b30 --- /dev/null +++ b/week5/KBC/boj_13458.java @@ -0,0 +1,23 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.Arrays; + +public class Main { + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int n = Integer.parseInt(br.readLine()); + int[] studentsInClass = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + int[] input = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); + int b = input[0]; + int c = input[1]; + + long totalSupervisors = 0; + for(int i=0; i