반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

코딩하기 좋은날

백준 2343 Dance Dance Revolution 본문

백준(Baekjoon) 문제

백준 2343 Dance Dance Revolution

huiung 2020. 1. 19. 12:38
반응형

 

dp 문제입니다. 각 지시사항때 두 발의 위치를 각각 인덱스로 주면 해결 할 수 있습니다.

 

즉 dp[i][j][k] = i지시사항때 왼발이 j 오른발이 k 라고 생각하시면 됩니다. 이후 각 함수마다 

 

다음 지시사항을 왼발을 이용해서 진행할지 오른발을 이용해서 진행할지 처리해주시면 해결이 됩니다.

 

아래는 코드입니다.

 

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>

#define INF 987654321
using namespace std;

int arr[100010];
int dp[100010][5][5]; //i번째 지시사항때 두발의 위치에 따른 사용힘
int idx;

int sol(int left, int right, int cnt) {
			
	if(cnt == idx) {				
		return 0;
	}
	
	int& ret = dp[cnt][left][right];
	if(ret != -1) return ret;
	ret = INF;
	
		
	int plus;
	if (left == arr[cnt]) plus = 1;
	else if (left == 0) plus = 2;
	else if (abs(left - arr[cnt]) == 2) plus = 4;
	else plus = 3;	
	
	ret = min(ret ,sol(arr[cnt], right, cnt+1)+plus);
	
	if (right == arr[cnt]) plus = 1;
	else if (right == 0) plus = 2;
	else if (abs(right - arr[cnt]) == 2) plus = 4;
	else plus = 3;	
	
	ret = min(ret, sol(left, arr[cnt], cnt+1)+plus);
	
	return ret;
}

int main(void) {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	memset(dp,-1,sizeof(dp));

	int x;	
	while (1) {
		cin >> x;
		if (x == 0) break;
		arr[idx++] = x;
	}	

	cout<<sol(0, 0, 0);
	return 0;
}
반응형

'백준(Baekjoon) 문제' 카테고리의 다른 글

백준 16236 아기 상어  (0) 2020.01.21
백준 2449 전구  (0) 2020.01.19
백준 7578 공장  (0) 2020.01.19
백준 1102 발전소  (0) 2020.01.19
백준 2618 경찰차  (2) 2020.01.19