Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions 0224LJH/202508/27 BOJ 마라톤 2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
```java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;


public class Main {

static int checkCnt, skipMaxCnt,ans;
static int[][] dp; //dp[i][j] skip j번하고 i번째 체크포인트에 도달했을 때의 최댓값
static int[] y;
static int[] x;

public static void main(String[] args) throws IOException {
init();
process();
print();
}

public static void init() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());

checkCnt = Integer.parseInt(st.nextToken());
skipMaxCnt = Integer.parseInt(st.nextToken());
dp = new int[checkCnt][skipMaxCnt+1];
y = new int[checkCnt];
x = new int[checkCnt];

for (int i = 0; i < checkCnt; i++) {
st = new StringTokenizer(br.readLine());
y[i] = Integer.parseInt(st.nextToken());
x[i] = Integer.parseInt(st.nextToken());

Arrays.fill( dp[i], Integer.MAX_VALUE/2);
}


dp[0][0] = 0;
ans = Integer.MAX_VALUE/2;
}

public static void process() throws IOException {
for (int i = 0; i < checkCnt; i++) {
int curY = y[i];
int curX = x[i];
for (int j = 0; j <=skipMaxCnt; j++) { // 이전에 스킵했던 횟수
for (int k = 0; k <= skipMaxCnt; k++) { // 이번에 스킵하려는 횟수
int nIdx = i+1+k;
int nSkipCnt = j + k;

if (nIdx >= checkCnt || nSkipCnt > skipMaxCnt) break;

int nY = y[nIdx];
int nX = x[nIdx];
int plus = Math.abs(curX- nX) + Math.abs(curY - nY);

dp[nIdx][nSkipCnt] = Math.min(dp[nIdx][nSkipCnt], dp[i][j] + plus);
}



}
}

for (int i = 0; i <= skipMaxCnt; i++) {

ans = Math.min(dp[checkCnt-1][i], ans);
}

}



public static void print() {
System.out.println(ans);
}
}

```