From 76da579511dd490a7e20ec8ba7c88d3cbee74a6f Mon Sep 17 00:00:00 2001 From: lkhyun <102892446+lkhyun@users.noreply.github.com> Date: Sat, 16 Aug 2025 18:54:42 +0900 Subject: [PATCH] =?UTF-8?q?[20250816]=20BOJ=20/=20G2=20/=20=EC=86=8C?= =?UTF-8?q?=EA=B0=80=20=EA=B8=B8=EC=9D=84=20=EA=B1=B4=EB=84=88=EA=B0=84=20?= =?UTF-8?q?=EC=9D=B4=EC=9C=A0=207=20/=20=EC=9D=B4=EA=B0=95=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...352\260\204 \354\235\264\354\234\240 7.md" | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 "lkhyun/202508/16 BOJ G2 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 7.md" diff --git "a/lkhyun/202508/16 BOJ G2 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 7.md" "b/lkhyun/202508/16 BOJ G2 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 7.md" new file mode 100644 index 00000000..69587734 --- /dev/null +++ "b/lkhyun/202508/16 BOJ G2 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 7.md" @@ -0,0 +1,72 @@ +```java +import java.io.*; +import java.util.*; + +public class Main { + static int N,T; + static int[][] farm; + static long[][][] dist; + static int[] di = {0,0,-1,1}; + static int[] dj = {-1,1,0,0}; + static long ans; + + public static void main(String[] args) throws IOException { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + StringTokenizer st = new StringTokenizer(br.readLine()); + N = Integer.parseInt(st.nextToken()); + T = Integer.parseInt(st.nextToken()); + farm = new int[N][N]; + + for (int i = 0; i < N; i++) { + st = new StringTokenizer(br.readLine()); + for (int j = 0; j < N; j++) { + farm[i][j] = Integer.parseInt(st.nextToken()); + } + } + + dijkstra(); + bw.write(ans+""); + bw.close(); + } + + static void dijkstra(){ + dist = new long[N][N][3]; + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + Arrays.fill(dist[i][j],Long.MAX_VALUE); + } + } + dist[0][0][0] = 0; + PriorityQueue pq = new PriorityQueue<>((a,b) -> Long.compare(a[2],b[2])); + pq.offer(new long[]{0,0,0,0}); // a,b,c,d : [a][b]까지 가는데 c만큼 걸리고 길 건넌 횟수는 d이다. + + while(!pq.isEmpty()){ + long[] cur = pq.poll(); + + if(dist[(int)cur[0]][(int)cur[1]][(int)cur[3]] < cur[2]) continue; + + for (int k = 0; k < 4; k++) { + int newi = (int)(cur[0] + di[k]); + int newj = (int)(cur[1] + dj[k]); + if(check(newi,newj)){ + long newDist = cur[2] + T; + int newCross = (int)(cur[3]+1) % 3; + if(newCross == 0){ + newDist += farm[newi][newj]; + } + if(newDist < dist[newi][newj][newCross]){ + dist[newi][newj][newCross] = newDist; + pq.offer(new long[]{newi,newj,newDist,newCross}); + } + } + } + } + ans = Math.min(dist[N-1][N-1][0],Math.min(dist[N-1][N-1][1], dist[N-1][N-1][2])); + } + static boolean check(long a, long b){ + if(a<0 || a>=N || b<0 || b>=N) return false; + return true; + } +} +```