Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 정렬
- VR
- ue5
- XR Interaction Toolkit
- 우선순위 큐
- 시뮬레이션
- 다이나믹 프로그래밍
- 그리디 알고리즘
- 알고리즘
- 브루트포스
- Team Fortress 2
- 구현
- 수학
- 누적 합
- 백트래킹
- 유니온 파인드
- 자료구조
- 재귀
- c++
- 문자열
- BFS
- Unreal Engine 5
- 유니티
- 트리
- 다익스트라
- 백준
- 스택
- DFS
- 투 포인터
- 그래프
Archives
- Today
- Total
1일1알
백준 2342번 Dance Dance Revolution C++ 본문
상당히 어려운 dp문제였다. cache[ i ][ j ][ k ] 를 i번째 지시사항에서 왼쪽발은 j, 오른쪽발은 k일때의 값으로 하고 풀었다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
const int INF = 0x3fffffff;
int cnt = 0;
int power[5][5] = {
{INF,2,2,2,2},
{INF,1,3,4,3},
{INF,3,1,3,4},
{INF,4,3,1,3},
{INF,3,4,3,1},
};
vector<vector<vector<int>>> cache;
vector<int> orders;
int GetPower(int cnt, int left, int right) {
if (cnt == 0) {
if (left == 0 && right == 0)
return 0;
else return INF;
}
int &ret = cache[cnt][left][right];
if (ret != -1)
return ret;
ret = INF;
if (orders[cnt] != left && orders[cnt] != right)
return ret;
for (int i = 0; i < 5; i++) {
if (power[i][left] != INF) {
int lastPower = GetPower(cnt - 1, i, right);
if (lastPower != INF) {
ret = min(ret, lastPower + power[i][left]);
}
}
if (power[i][right] != INF) {
int lastPower = GetPower(cnt - 1, left, i);
if (lastPower != INF) {
ret = min(ret, lastPower + power[i][right]);
}
}
}
return ret;
}
void Init() {
cache = vector<vector<vector<int>>>(cnt + 1, vector<vector<int>>(5, vector<int>(5, -1)));
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
cache[0][i][j] = INF;
}
}
for (int i = 1; i <= cnt; i++) {
cache[i][0][0] = INF;
cache[i][1][1] = INF;
cache[i][2][2] = INF;
cache[i][3][3] = INF;
cache[i][4][4] = INF;
}
cache[0][0][0] = 0;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int ans = INF;
orders.push_back(0);
while (true) {
int order;
cin >> order;
if (order == 0) break;
orders.push_back(order);
cnt++;
}
if (cnt == 0) {
cout << 0;
return 0;
}
Init();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
ans = min(ans, GetPower(cnt, i, j));
}
}
cout << ans;
}
'알고리즘' 카테고리의 다른 글
백준 16946번 벽 부수고 이동하기 4 C++ (0) | 2022.07.23 |
---|---|
백준 16724번 피리 부는 사나이 C++ (0) | 2022.07.22 |
백준 2143번 두 배열의 합 C++ (0) | 2022.07.20 |
백준 1644번 소수의 연속합 C++ (0) | 2022.07.19 |
Judge - 1231 : 양궁 선수 순위 예측 C++ (0) | 2022.07.18 |