From 8f659b6905097b42d96440ebc581c42588edeab3 Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sat, 6 Dec 2025 20:23:19 +0900 Subject: [PATCH] =?UTF-8?q?[20251206]=20BOJ=20/=20G4=20/=20=EC=9A=B0?= =?UTF-8?q?=EC=9C=A0=20=EB=8F=84=EC=8B=9C=20/=20=EC=9D=B4=EA=B0=95?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\234\240 \353\217\204\354\213\234.md" | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 "lkhyun/202512/06 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" diff --git "a/lkhyun/202512/06 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" "b/lkhyun/202512/06 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" new file mode 100644 index 00000000..ab7f749f --- /dev/null +++ "b/lkhyun/202512/06 BOJ G4 \354\232\260\354\234\240 \353\217\204\354\213\234.md" @@ -0,0 +1,97 @@ +```java +import java.util.*; +import java.io.*; + +public class Main{ + static class State{ + int lastMilk; + int cnt; + State(int lastMilk,int cnt){ + this.lastMilk = lastMilk; + this.cnt = cnt; + } + } + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st; + static StringBuilder sb = new StringBuilder(); + static int N; + static int[][] stores; + static State[][] dp; + + public static void main(String[] args) throws Exception { + N = Integer.parseInt(br.readLine()); + stores = new int[N+1][N+1]; + + for(int i = 1; i<=N; i++){ + st = new StringTokenizer(br.readLine()); + for(int j = 1; j <= N; j++){ + stores[i][j] = Integer.parseInt(st.nextToken()); + } + } + + dp = new State[N+1][N+1]; + + if(stores[1][1] == 0){ + dp[1][1] = new State(0, 1); + }else{ + dp[1][1] = new State(-1, 0); + } + + + for(int j = 2; j <= N; j++){ + int nextMilk = (dp[1][j-1].lastMilk + 1) % 3; + if(dp[1][j-1].lastMilk == -1) nextMilk = 0; + + if(stores[1][j] == nextMilk){ + dp[1][j] = new State(stores[1][j], dp[1][j-1].cnt + 1); + }else{ + dp[1][j] = new State(dp[1][j-1].lastMilk, dp[1][j-1].cnt); + } + } + + for(int i = 2; i <= N; i++){ + int nextMilk = (dp[i-1][1].lastMilk + 1) % 3; + if(dp[i-1][1].lastMilk == -1) nextMilk = 0; + + if(stores[i][1] == nextMilk){ + dp[i][1] = new State(stores[i][1], dp[i-1][1].cnt + 1); + }else{ + dp[i][1] = new State(dp[i-1][1].lastMilk, dp[i-1][1].cnt); + } + } + + for(int i = 2; i<=N;i++){ + for(int j = 2; j<=N; j++){ + State up = dp[i-1][j]; + State left = dp[i][j-1]; + + int upNextMilk = (up.lastMilk + 1) % 3; + if(up.lastMilk == -1) upNextMilk = 0; + + int leftNextMilk = (left.lastMilk + 1) % 3; + if(left.lastMilk == -1) leftNextMilk = 0; + + int upCnt = up.cnt + (stores[i][j] == upNextMilk ? 1 : 0); + int leftCnt = left.cnt + (stores[i][j] == leftNextMilk ? 1 : 0); + + if(upCnt >= leftCnt){ + if(stores[i][j] == upNextMilk){ + dp[i][j] = new State(stores[i][j], upCnt); + }else{ + dp[i][j] = new State(up.lastMilk, upCnt); + } + }else{ + if(stores[i][j] == leftNextMilk){ + dp[i][j] = new State(stores[i][j], leftCnt); + }else{ + dp[i][j] = new State(left.lastMilk, leftCnt); + } + } + } + } + bw.write(dp[N][N].cnt+""); + bw.close(); + } +} +```