본문 바로가기
PS(Problem Solving)/백준_BOJ

[백준] 10653번 - 마라톤 2 (C++) 문제 및 풀이

by 초코칩프라푸치노 2022. 1. 17.

문제) 백준 - 동적 계획법 - 마라톤 2

https://www.acmicpc.net/problem/10653

 

10653번: 마라톤 2

젖소 박승원이 2번째 와 4번째 체크포인트를 건너뛸경우 경로는 (0,0) -> (1,1) -> (2,2) 로 4가 된다. 이보다 더 짧게 달릴수는 없다.

www.acmicpc.net

 

모든 체크 포인트의 좌표를 입력 받아 거리를 미리 계산합니다. 그 후, 체크 포인트를 뛰어넘는 경우와 그렇지 않는 경우로 각 최소 거리를 계산합니다.

 

C++ 소스코드)

int solve(int idx, int ck) {
if (idx == 0) return 0;
int& ret = cache[idx][ck];
if (ret != -1) return ret;
ret = INF;
for (int i = 0; i <= ck; ++i)
if(idx - i - 1 >= 0)
ret = min(ret, solve(idx - 1 - i, ck - i) + dist[idx -1 -i][idx]);
return ret;
}
view raw 10653.cpp hosted with ❤ by GitHub

 

 

Full Code)

https://github.com/Chocochip101/BOJ_Solution/blob/main/Problem%2010000~14999/10653_%EB%A7%88%EB%9D%BC%ED%86%A4%202.cpp

 

GitHub - Chocochip101/BOJ_Solution: BOJ Solutions

BOJ Solutions. Contribute to Chocochip101/BOJ_Solution development by creating an account on GitHub.

github.com

 

반응형

댓글