From 3163962f427cbdc439227a1d68a50e727bc2f1c5 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:31:39 +0900 Subject: [PATCH] =?UTF-8?q?[20250213]=20BOJ=20/=20=EA=B3=A8=EB=93=9C3=20/?= =?UTF-8?q?=20=EC=BC=80=EC=9D=B5=20=EB=B0=B0=EB=8B=AC=20/=20=EA=B9=80?= =?UTF-8?q?=EC=88=98=EC=97=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...0\354\235\265 \353\260\260\353\213\254.md" | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 "suyeun84/202502/13 BOJ G3 \354\274\200\354\235\265 \353\260\260\353\213\254.md" diff --git "a/suyeun84/202502/13 BOJ G3 \354\274\200\354\235\265 \353\260\260\353\213\254.md" "b/suyeun84/202502/13 BOJ G3 \354\274\200\354\235\265 \353\260\260\353\213\254.md" new file mode 100644 index 00000000..e05bd29b --- /dev/null +++ "b/suyeun84/202502/13 BOJ G3 \354\274\200\354\235\265 \353\260\260\353\213\254.md" @@ -0,0 +1,60 @@ +```java +import java.util.*; +import java.io.*; + +class Solution +{ + public static void main(String[] args) throws Exception{ + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + StringTokenizer st = new StringTokenizer(br.readLine()); + + int N = Integer.parseInt(st.nextToken()); + ArrayList target = new ArrayList<>(); + long[][] dp = new long[N+1][5]; // 0:정위치 1:북 2:동 3:남 4:서 + int[][] dir = new int[][] {{0,0}, {-1,0}, {0,1}, {1,0}, {0,-1}}; + long answer = Long.MAX_VALUE; + + for (int i = 0; i< N+1; i++) { + st = new StringTokenizer(br.readLine()); + target.add(new Position(Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()))); + Arrays.fill(dp[i], Long.MAX_VALUE); + } + for (int i = 0 ; i < 5; i++) { + if (i == 0) { + dp[0][0] = 0; + } else { + dp[0][i] = 1; + } + } + + for (int i = 1; i < N+1; i++) { + for (int j = 0; j < 5; j++) { + //이전꺼 위치 + int y = target.get(i-1).y + dir[j][0]; + int x = target.get(i-1).x + dir[j][1]; + for (int z = 0; z < 5; z++) { + //이번꺼 위치 + int ty = target.get(i).y + dir[z][0]; + int tx = target.get(i).x + dir[z][1]; + //목표 위치의 상하좌우 + dp[i][z] = Math.min(dp[i][z], Math.abs(y-ty)+Math.abs(x-tx)+dp[i-1][j]); + } + } + } + for (int i = 0; i < 5; i++) { + answer = Math.min(answer, dp[N][i]); + } + System.out.println(answer); + } + + static class Position { + int y; + int x; + public Position(int y, int x) { + this.y = y; + this.x = x; + } + } +} + +```