From 5b27d40b090e67985f8ecb8a6cda2222d2d75c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=BD=EB=AF=B8=EB=9E=98?= Date: Tue, 7 May 2024 08:42:09 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20BOJ=5F16197=5F=EB=91=90=EB=8F=99?= =?UTF-8?q?=EC=A0=84=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\263\275\353\257\270\353\236\230.java" | 121 ++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 "4\354\243\274\354\260\250/\353\221\220\353\217\231\354\240\204/\353\221\220\353\217\231\354\240\204_\352\263\275\353\257\270\353\236\230.java" diff --git "a/4\354\243\274\354\260\250/\353\221\220\353\217\231\354\240\204/\353\221\220\353\217\231\354\240\204_\352\263\275\353\257\270\353\236\230.java" "b/4\354\243\274\354\260\250/\353\221\220\353\217\231\354\240\204/\353\221\220\353\217\231\354\240\204_\352\263\275\353\257\270\353\236\230.java" new file mode 100644 index 0000000..f77fabe --- /dev/null +++ "b/4\354\243\274\354\260\250/\353\221\220\353\217\231\354\240\204/\353\221\220\353\217\231\354\240\204_\352\263\275\353\257\270\353\236\230.java" @@ -0,0 +1,121 @@ +package boj; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayDeque; +import java.util.Queue; +import java.util.StringTokenizer; + +/** + * 문제] + * NxM 보드, 두개의 동전 + * 버튼은 상하좌우 이동 4가지 + * 벽 -> 동전 이동 x + * 칸이 없으면 동전은 떨어짐 + * 두 동전 중 하나만 보드에서 떨어뜨리는 최소 횟수 + * + * 조건] + * 1 <= N, M <= 20 + * 보드 상태 + * - o : 동전 + * - . : 빈칸 + * - # : 벽 + * 두 동전을 떨어뜨릴 수 없거나 버튼을 10번보다 많이 눌러야하면 -1 출력 + * + * 풀이] + * 구현 문제 + * 4방향 돌면서 BFS 때려 + * + */ +public class boj_16197_두동전 { + static class Point { + int r, c; + public Point(int r, int c) { + this.r = r; + this.c = c; + } + } + + static class Coin{ + int count; + Point coin1; + Point coin2; + + public Coin(int count, Point coin1, Point coin2) { + this.count = count; + this.coin1 = coin1; + this.coin2 = coin2; + } + } + + static int N, M, MIN_COUNT = 11; + static char[][] map; + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + map = new char[N][M]; + Point coin1 = null; + Point coin2 = null; + for (int i = 0; i < N; i++) { + String line = br.readLine(); + for (int j = 0; j < M; j++) { + map[i][j] = line.charAt(j); + if(map[i][j] == 'o') { + if(coin1 == null) coin1 = new Point(i, j); + else if(coin2 == null) coin2 = new Point(i, j); + } + } + } + + dropCoin(new Coin(0, coin1, coin2)); + System.out.println(MIN_COUNT == 11 ? -1 : MIN_COUNT); + } + + static int[] dr = {-1, 0, 1, 0}; + static int[] dc = {0, 1, 0, -1}; + public static void dropCoin(Coin coin) { + boolean[][][][] visited = new boolean[N][M][N][M]; + Queue queue = new ArrayDeque<>(); + queue.add(coin); + visited[coin.coin1.r][coin.coin1.c][coin.coin2.r][coin.coin2.c] = true; + while (!queue.isEmpty()) { + Coin curr = queue.poll(); + if(curr.count >= 10) break; + + for(int d=0; d<4; d++) { + int nr1 = curr.coin1.r + dr[d]; + int nc1 = curr.coin1.c + dc[d]; + int nr2 = curr.coin2.r + dr[d]; + int nc2 = curr.coin2.c + dc[d]; + + if(!isOutOfRange(nr1, nc1) && map[nr1][nc1] == '#') { + nr1 = curr.coin1.r; + nc1 = curr.coin1.c; + } + if(!isOutOfRange(nr2, nc2) && map[nr2][nc2] == '#') { + nr2 = curr.coin2.r; + nc2 = curr.coin2.c; + } + + if(isOutOfRange(nr1, nc1) && isOutOfRange(nr2, nc2)) continue; + else if(isOutOfRange(nr1, nc1) || isOutOfRange(nr2, nc2)) { + MIN_COUNT = Math.min(MIN_COUNT, curr.count+1); + return; + } + + if(!visited[nr1][nc1][nr2][nc2]) { + queue.add(new Coin(curr.count+1, new Point(nr1, nc1), new Point(nr2, nc2))); + visited[nr1][nc1][nr2][nc2] = true; + } + } + } + } + + public static boolean isOutOfRange(int r, int c) { + return r < 0 || r >= N || c < 0 || c >= M; + } +} From 39b29f2f5b639d8e96466ae21129ee3f298df6de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=BD=EB=AF=B8=EB=9E=98?= Date: Tue, 7 May 2024 08:42:24 +0900 Subject: [PATCH 2/4] =?UTF-8?q?feat:=20BOJ=5F17837=5F=EC=83=88=EB=A1=9C?= =?UTF-8?q?=EC=9A=B4=EA=B2=8C=EC=9E=842=20=EC=95=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=EC=A6=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\263\275\353\257\270\353\236\230.java" | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 "4\354\243\274\354\260\250/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042_\352\263\275\353\257\270\353\236\230.java" diff --git "a/4\354\243\274\354\260\250/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042_\352\263\275\353\257\270\353\236\230.java" "b/4\354\243\274\354\260\250/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042_\352\263\275\353\257\270\353\236\230.java" new file mode 100644 index 0000000..f99b915 --- /dev/null +++ "b/4\354\243\274\354\260\250/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042/\354\203\210\353\241\234\354\232\264\352\262\214\354\236\2042_\352\263\275\353\257\270\353\236\230.java" @@ -0,0 +1,138 @@ +package boj; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.StringTokenizer; + +/** + * 문제] + * NXN 체스판, K개의 말 + * 상하좌우 이동 + * 한턴에서 1번 말부터 K번까지 차례로 이동 + * 한 말이 이동할 때 올려져있는 말도 함께 이동 + * 말이 4개 이상 쌓이는 순간 게임 종료 + * + * A번 말 이동 룰 + * 1. 흰색인 경우 그 칸으로 이동. 이동하려는 칸에 말이 있으면 겹침 + * - 말 위에 다른 말이 있을 경우 함께 이동 + * 2. 빨간색인 경우 이동한 후 A번 말과 그 위에 말들의 쌓여있는 순서를 바꾼다 + * 3. 파란색인 경우 A번 말의 이동방향을 반대로 하고 한칸 이동한다. 방향을 바꾼 후 칸이 파란색이면 이동하지 않고 가만히 있는다. + * 4. 체스판을 벗어나는 경우 파란색과 같은 경우 + * + * 조건] + * 4 <= N <= 12 : 체스판 크기 + * 4 <= K <= 10 : 말의 개수 + * 0:흰색, 1:빨간색, 2:파란색 + * 이동방향 = 1:우, 2:좌, 3:상, 4:하 + * 게임이 종료되는 턴의 번호 출력 + * 1000보다 크거나 종료되지 않으면 -1 출력 + * + * 풀이] + * + * + */ +public class boj_17837_새로운게임2 { + static int N, K; + static int[][] map, horse; + static List[][] piece; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + K = Integer.parseInt(st.nextToken()); + + map = new int[N+1][N+1]; + piece = new List[N+1][N+1]; + for (int i = 1; i <= N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 1; j <= N; j++) { + piece[i][j] = new ArrayList<>(); + map[i][j] = Integer.parseInt(st.nextToken()); + } + } + horse = new int[K+1][3]; + for (int i = 1; i <= K; i++) { + st = new StringTokenizer(br.readLine()); + int r = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + horse[i][0] = r; + horse[i][1] = c; + horse[i][2] = Integer.parseInt(st.nextToken()); + + piece[r][c].add(i); + } + + System.out.println(simulation()); + } + + private static int simulation() { + int[] dr = {0, 0, 0, -1, 1}; + int[] dc = {0, 1, -1, 0, 0}; + + int count = 0; + while(count < 1000) { + count++; + + for(int i=1; i<=K; i++) { + int r = horse[i][0]; + int c = horse[i][1]; + int d = horse[i][2]; + + int nr = r + dr[d]; + int nc = c + dc[d]; + + if(isOutOfRange(nr, nc)) { + int nd = d % 2 == 0 ? d -1 : d + 1; + horse[i][2] = nd; + if(map[r+dr[nd]][c+dc[nd]] != 2) i--; + continue; + } + + if(map[nr][nc] == 0) { + int idx = piece[r][c].indexOf(i); + piece[nr][nc].addAll(piece[r][c].subList(idx, piece[r][c].size())); + piece[r][c] = piece[r][c].subList(0, idx); + horse[i][0] = nr; + horse[i][1] = nc; + for(Integer j: piece[nr][nc]) { + horse[j][0] = nr; + horse[j][1] = nc; + } + } else if(map[nr][nc] == 1) { + int idx = piece[r][c].indexOf(i); + List temp = piece[r][c].subList(idx, piece[r][c].size()); + Collections.reverse(temp); + + piece[nr][nc].addAll(temp); + piece[r][c] = piece[r][c].subList(0, idx); + for(Integer j: piece[nr][nc]) { + horse[j][0] = nr; + horse[j][1] = nc; + } + } else if(map[nr][nc] == 2) { + int nd = d % 2 == 0 ? d -1 : d + 1; + horse[i][2] = nd; + + if(isOutOfRange(r+dr[nd], c+dc[nd])) continue; + + if(map[r+dr[nd]][c+dc[nd]] != 2) i--; + } + + if(piece[nr][nc].size() > 3) { + return count; + } + } + } + return -1; + } + + private static boolean isOutOfRange(int nr, int nc) { + return nr < 1 || nr > N || nc < 1 || nc > N; + } +} From 2c06c3262b2dac014398033507fd04fd0f076796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=BD=EB=AF=B8=EB=9E=98?= Date: Tue, 7 May 2024 08:42:38 +0900 Subject: [PATCH 3/4] =?UTF-8?q?feat:=20BOJ=5F2176=5F=ED=95=A9=EB=A6=AC?= =?UTF-8?q?=EC=A0=81=EC=9D=B8=EC=9D=B4=EB=8F=99=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\263\275\353\257\270\353\236\230.java" | 135 ++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 "4\354\243\274\354\260\250/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234_\352\263\275\353\257\270\353\236\230.java" diff --git "a/4\354\243\274\354\260\250/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234_\352\263\275\353\257\270\353\236\230.java" "b/4\354\243\274\354\260\250/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234_\352\263\275\353\257\270\353\236\230.java" new file mode 100644 index 0000000..d1ba2a6 --- /dev/null +++ "b/4\354\243\274\354\260\250/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234/\355\225\251\353\246\254\354\240\201\354\235\270\354\235\264\353\217\231\352\262\275\353\241\234_\352\263\275\353\257\270\353\236\230.java" @@ -0,0 +1,135 @@ +package boj; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.PriorityQueue; +import java.util.StringTokenizer; + +/** + * 문제] + * S에서 T로 이동 + * T에 가까워지며 이동하는 경우 합리적인 이동경로 + * 합리적인 이동경로는 최단거리가 아닐 수도 있음 + * 그래프에 대해 합리적인 이동경로 개수를 구해라 + * S=1, T=2 + * + * 조건] + * 정점의 개수 : 1 <= N <= 1000 + * 간선의 개수 : 1 <= M <= 100000 + * A : 정점 + * B : 정점 + * 간선 길이 : 0 < C <= 10000 + * + * 풀이] + * S에서 T로 갈때 X를 거칠 수 있다면 + * X-> T < S ->T 가 되어야 합리적인 이동경로 + * + * T에서 부터 각 노드까지의 비용을 계산 - 다익스트라 + * dp를 사용해서 각 노드에서 T까지의 합리적인 이동경로 개수 계산 + * 최단 거리가 짧은 노드부터 계산 + * + */ +public class boj_2176_합리적인이동경로 { + static int N, M; + static int[] dist; + static List[] edges; + + static class Node { + int next, cost; + public Node(int next, int cost) { + this.next = next; + this.cost = cost; + } + } + + static class Point implements Comparable{ + int index, dist; + public Point(int index, int dist) { + this.index = index; + this.dist = dist; + } + + @Override + public int compareTo(Point o) { + return this.dist - o.dist; + } + } + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + N = Integer.parseInt(st.nextToken()); + M = Integer.parseInt(st.nextToken()); + + edges = new List[N+1]; + for (int i=1; i<=N; i++) { + edges[i] = new ArrayList<>(); + } + + for (int i = 0; i < M; i++) { + st = new StringTokenizer(br.readLine()); + int a = Integer.parseInt(st.nextToken()); + int b = Integer.parseInt(st.nextToken()); + int c = Integer.parseInt(st.nextToken()); + + edges[a].add(new Node(b, c)); + edges[b].add(new Node(a, c)); + } + + + dijkstra(); + System.out.println(dp()); + } + + static void dijkstra() { + dist = new int[N+1]; + Arrays.fill(dist, Integer.MAX_VALUE); + dist[2] = 0; + boolean[] visited = new boolean[N+1]; + + PriorityQueue queue = new PriorityQueue<>(); + queue.add(new Point(2, 0)); + visited[2] = true; + + while (!queue.isEmpty()) { + Point curr = queue.poll(); + + for(Node next : edges[curr.index]) { + if(dist[next.next] > dist[curr.index] + next.cost) { + dist[next.next] = dist[curr.index] + next.cost; + queue.add(new Point(next.next, dist[next.next])); + } + } + } + } + + static int dp() { + PriorityQueue queue = new PriorityQueue<>(); + for (int i=1; i<=N; i++) { + queue.add(new Point(i, dist[i])); + } + queue.poll(); + + int[] dp = new int[N+1]; + dp[2] = 1; + + while (!queue.isEmpty()) { + Point curr = queue.poll(); + + for(Node node : edges[curr.index]) { + if(dist[node.next] < dist[curr.index]) { + dp[curr.index] += dp[node.next]; + } + } + + if(curr.index == 1) break; + } + + return dp[1]; + } +} From b343d466e0b4622c2a755b83e1ce1e62e51863fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B3=BD=EB=AF=B8=EB=9E=98?= Date: Tue, 7 May 2024 08:42:50 +0900 Subject: [PATCH 4/4] =?UTF-8?q?feat:=20PGMS=5F=EA=B0=80=EC=9E=A5=ED=81=B0?= =?UTF-8?q?=EC=88=98=20=EC=95=8C=EA=B3=A0=EB=A6=AC=EC=A6=98=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\263\275\353\257\270\353\236\230.java" | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 "4\354\243\274\354\260\250/\352\260\200\354\236\245\355\201\260\354\210\230/\352\260\200\354\236\245\355\201\260\354\210\230_\352\263\275\353\257\270\353\236\230.java" diff --git "a/4\354\243\274\354\260\250/\352\260\200\354\236\245\355\201\260\354\210\230/\352\260\200\354\236\245\355\201\260\354\210\230_\352\263\275\353\257\270\353\236\230.java" "b/4\354\243\274\354\260\250/\352\260\200\354\236\245\355\201\260\354\210\230/\352\260\200\354\236\245\355\201\260\354\210\230_\352\263\275\353\257\270\353\236\230.java" new file mode 100644 index 0000000..bf141cc --- /dev/null +++ "b/4\354\243\274\354\260\250/\352\260\200\354\236\245\355\201\260\354\210\230/\352\260\200\354\236\245\355\201\260\354\210\230_\352\263\275\353\257\270\353\236\230.java" @@ -0,0 +1,29 @@ +package programmers; + +import java.util.Arrays; + +public class PGMS_가장큰수 { + + public static String solution(int[] numbers) { + String[] nums = new String[numbers.length]; + for(int i=0; i (o1+o2).compareTo(o2+o1) * -1); + + if(nums[0].equals("0")) return "0"; + + String answer = ""; + for(int i=0; i