|
| 1 | +```java |
| 2 | +import java.io.BufferedReader; |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.StringTokenizer; |
| 9 | + |
| 10 | + |
| 11 | +public class Main { |
| 12 | + |
| 13 | + static int checkCnt, skipMaxCnt,ans; |
| 14 | + static int[][] dp; //dp[i][j] skip j번하고 i번째 체크포인트에 도달했을 때의 최댓값 |
| 15 | + static int[] y; |
| 16 | + static int[] x; |
| 17 | + |
| 18 | + public static void main(String[] args) throws IOException { |
| 19 | + init(); |
| 20 | + process(); |
| 21 | + print(); |
| 22 | + } |
| 23 | + |
| 24 | + public static void init() throws IOException { |
| 25 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 26 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 27 | + |
| 28 | + checkCnt = Integer.parseInt(st.nextToken()); |
| 29 | + skipMaxCnt = Integer.parseInt(st.nextToken()); |
| 30 | + dp = new int[checkCnt][skipMaxCnt+1]; |
| 31 | + y = new int[checkCnt]; |
| 32 | + x = new int[checkCnt]; |
| 33 | + |
| 34 | + for (int i = 0; i < checkCnt; i++) { |
| 35 | + st = new StringTokenizer(br.readLine()); |
| 36 | + y[i] = Integer.parseInt(st.nextToken()); |
| 37 | + x[i] = Integer.parseInt(st.nextToken()); |
| 38 | + |
| 39 | + Arrays.fill( dp[i], Integer.MAX_VALUE/2); |
| 40 | + } |
| 41 | + |
| 42 | + |
| 43 | + dp[0][0] = 0; |
| 44 | + ans = Integer.MAX_VALUE/2; |
| 45 | + } |
| 46 | + |
| 47 | + public static void process() throws IOException { |
| 48 | + for (int i = 0; i < checkCnt; i++) { |
| 49 | + int curY = y[i]; |
| 50 | + int curX = x[i]; |
| 51 | + for (int j = 0; j <=skipMaxCnt; j++) { // 이전에 스킵했던 횟수 |
| 52 | + for (int k = 0; k <= skipMaxCnt; k++) { // 이번에 스킵하려는 횟수 |
| 53 | + int nIdx = i+1+k; |
| 54 | + int nSkipCnt = j + k; |
| 55 | + |
| 56 | + if (nIdx >= checkCnt || nSkipCnt > skipMaxCnt) break; |
| 57 | + |
| 58 | + int nY = y[nIdx]; |
| 59 | + int nX = x[nIdx]; |
| 60 | + int plus = Math.abs(curX- nX) + Math.abs(curY - nY); |
| 61 | + |
| 62 | + dp[nIdx][nSkipCnt] = Math.min(dp[nIdx][nSkipCnt], dp[i][j] + plus); |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + for (int i = 0; i <= skipMaxCnt; i++) { |
| 71 | + |
| 72 | + ans = Math.min(dp[checkCnt-1][i], ans); |
| 73 | + } |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + public static void print() { |
| 80 | + System.out.println(ans); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +``` |
0 commit comments