https://www.acmicpc.net/problem/21317
21317번: 징검다리 건너기
산삼을 얻기 위해 필요한 영재의 최소 에너지를 출력한다.
www.acmicpc.net
package BOJ.dp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_21317 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String,Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int[] smallJump = new int[n];
int[] bigJump = new int[n];
for(int i = 1 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
smallJump[i] = stoi.apply(st.nextToken());
bigJump[i] = stoi.apply(st.nextToken());
}
int k = stoi.apply(br.readLine());
int[][] dp = new int[n][2];
int result = cal(1,NOT_USED,smallJump,bigJump,n,k,dp);
System.out.println(result);
}
private static final int INF = 987_654_321;
private static final int NOT_USED = 0;
private static final int USED = 1;
private static final int NOT_VALID = 0;
private static int cal(int now, int used,int[] smallJump, int[] bigJump, int n, int k,int[][]dp) {
int result = INF;
if(now == n){
return 0;
}
if(dp[now][used] != NOT_VALID){
return dp[now][used];
}
if(now + 1 <= n){
result = Math.min(result,cal(now+1,used,smallJump,bigJump,n,k,dp) + smallJump[now]);
}
if(now + 2 <= n){
result = Math.min(result,cal(now+2,used,smallJump,bigJump,n,k,dp) + bigJump[now]);
}
if(used == NOT_USED && now + 3 <= n){
result = Math.min(result,cal(now+3,USED,smallJump,bigJump,n,k,dp) + k);
}
dp[now][used] = result;
return result;
}
}
'알고리즘' 카테고리의 다른 글
프로그래머스 배열 뒤집기 (JAVA) (0) | 2023.02.25 |
---|---|
백준 17266번 어두운 굴다리 (JAVA) (0) | 2023.02.24 |
백준 17204번 죽음의 게임 (JAVA) (0) | 2023.02.22 |
백준 5212번 지구 온난화 (JAVA) (0) | 2023.02.21 |
백준 1197번 최소 스패닝 트리 (JAVA) (0) | 2023.02.20 |