From 3f9f3ee89028e364648c237fb08f9d473864fc1c Mon Sep 17 00:00:00 2001 From: nyanpasu-life <118531617+nyanpasu-life@users.noreply.github.com> Date: Sun, 28 Apr 2024 19:04:53 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Create=20PGMS=5F=EC=88=98=EB=A0=88=5F?= =?UTF-8?q?=EC=9B=80=EC=A7=81=EC=9D=B4=EA=B8=B0.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\354\247\201\354\235\264\352\270\260.java" | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 "\355\225\234\355\203\234\355\235\254/0430/PGMS_\354\210\230\353\240\210_\354\233\200\354\247\201\354\235\264\352\270\260.java" diff --git "a/\355\225\234\355\203\234\355\235\254/0430/PGMS_\354\210\230\353\240\210_\354\233\200\354\247\201\354\235\264\352\270\260.java" "b/\355\225\234\355\203\234\355\235\254/0430/PGMS_\354\210\230\353\240\210_\354\233\200\354\247\201\354\235\264\352\270\260.java" new file mode 100644 index 0000000..6fea221 --- /dev/null +++ "b/\355\225\234\355\203\234\355\235\254/0430/PGMS_\354\210\230\353\240\210_\354\233\200\354\247\201\354\235\264\352\270\260.java" @@ -0,0 +1,109 @@ +import java.util.*; + +class Solution { + int N, M; + int[][] maze; + boolean[][] rV, bV; + int answer = Integer.MAX_VALUE; + + final int[] dr = {1, -1, 0, 0}; + final int[] dc = {0, 0, 1, -1}; + + public int solution(int[][] m) { + maze = m; + N = maze.length; + M = maze[0].length; + + int redR=-1, redC=-1, blueR=-1, blueC=-1; + + for(int i=0;i nextRedCords = new ArrayList<>(); + List nextBlueCords = new ArrayList<>(); + + if(redEnd){ + nextRedCords.add(new int[]{redR, redC}); + } + else{ + for(int k=0;k<4;k++){ + int nr = redR + dr[k]; + int nc = redC + dc[k]; + if(isValid(nr, nc)==false || rV[nr][nc]==true || maze[nr][nc]==5) continue; + nextRedCords.add(new int[]{nr, nc}); + } + } + if(blueEnd){ + nextBlueCords.add(new int[]{blueR, blueC}); + } + else{ + for(int k=0;k<4;k++){ + int nr = blueR + dr[k]; + int nc = blueC + dc[k]; + if(isValid(nr, nc)==false || bV[nr][nc]==true || maze[nr][nc]==5) continue; + nextBlueCords.add(new int[]{nr, nc}); + } + } + + for(int[] nextRedCord: nextRedCords){ + for(int[] nextBlueCord: nextBlueCords){ + + int rnr = nextRedCord[0], rnc = nextRedCord[1], bnr = nextBlueCord[0], bnc = nextBlueCord[1]; + + //위치가 교환 되는 경우 스킵 + if(rnr==blueR && rnc==blueC && bnr==redR && bnc==redC) continue; + + //두 공의 위치가 겹칠 경우 스킵 + if(rnr==bnr && rnc==bnc) continue; + + rV[rnr][rnc] = true; + bV[bnr][bnc] = true; + + boolean nextRedEnd = false, nextBlueEnd = false; + if(maze[rnr][rnc]==3) nextRedEnd = true; + if(maze[bnr][bnc]==4) nextBlueEnd = true; + + dfs(step+1, rnr, rnc, bnr, bnc, nextRedEnd, nextBlueEnd); + + rV[rnr][rnc] = false; + bV[bnr][bnc] = false; + } + } + + } + + boolean isValid(int r, int c){ + if(0<=r && r Date: Tue, 30 Apr 2024 07:29:49 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=ED=95=9C=ED=83=9C=ED=9D=AC=5F0430?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...\352\270\260\352\264\200\354\260\250.java" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "\355\225\234\355\203\234\355\235\254/0430/Main_bj_g3_2616_\354\206\214\355\230\225\352\270\260\352\264\200\354\260\250.java" diff --git "a/\355\225\234\355\203\234\355\235\254/0430/Main_bj_g3_2616_\354\206\214\355\230\225\352\270\260\352\264\200\354\260\250.java" "b/\355\225\234\355\203\234\355\235\254/0430/Main_bj_g3_2616_\354\206\214\355\230\225\352\270\260\352\264\200\354\260\250.java" new file mode 100644 index 0000000..bbda435 --- /dev/null +++ "b/\355\225\234\355\203\234\355\235\254/0430/Main_bj_g3_2616_\354\206\214\355\230\225\352\270\260\352\264\200\354\260\250.java" @@ -0,0 +1,43 @@ +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.IOException; + +public class Main_bj_g3_2616_소형기관차 { + static int[] passengers; + static Integer[][] dp; + static int[] sum; + static int maxCarry; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + + int n = Integer.parseInt(br.readLine()); + passengers = new int[n+1]; + dp = new Integer[n+1][4]; + sum = new int[n+1]; + + String[] input = br.readLine().split(" "); + for (int i = 1; i <= n; i++) { + passengers[i] = Integer.parseInt(input[i-1]); + sum[i] = sum[i-1] + passengers[i]; + } + + maxCarry = Integer.parseInt(br.readLine()); + + System.out.println(findMaxPassenger(0, 3)); + } + + public static int findMaxPassenger(int start, int trainCount) { + if (trainCount == 0) return 0; + + if (dp[start][trainCount] == null) { + dp[start][trainCount] = 0; + + for (int i = start + 1; i <= passengers.length - maxCarry * trainCount; i++) { + dp[start][trainCount] = Math.max(dp[start][trainCount], findMaxPassenger(i + maxCarry - 1, trainCount - 1) + (sum[i + maxCarry - 1] - sum[start])); + } + } + + return dp[start][trainCount]; + } +} \ No newline at end of file