Skip to content

Commit aaee4f4

Browse files
committed
[BOJ] 2240 자두나무 (G4)
1 parent 4ba7ca7 commit aaee4f4

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

박예진/2주차/260107.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//https://www.acmicpc.net/submit/2240/97116669
2+
3+
#include <iostream>
4+
#include <vector>
5+
#include <algorithm>
6+
#include <cstring>
7+
using namespace std;
8+
9+
int T, W;
10+
int dp[1001][31][2]; // 시간, 움직인 횟수, idx
11+
vector<int> tree;
12+
13+
int dfs(int depth, int cnt, int idx) {
14+
// 기저 조건
15+
if (depth == T) return 0;
16+
if (cnt < 0) return -1e9; // 불가능한 조건
17+
18+
// 메모이제이션
19+
int &res = dp[depth][cnt][idx];
20+
if (res != -1) return res;
21+
22+
// 로직
23+
int move = dfs(depth + 1, cnt - 1, idx ^ 1); // 움직
24+
int nomove = dfs(depth + 1, cnt, idx); // 안움직
25+
res = max(move, nomove) + (idx == tree[depth] - 1);
26+
27+
return res;
28+
}
29+
30+
int main() {
31+
ios::sync_with_stdio(false);
32+
cin.tie(0); cout.tie(0);
33+
34+
cin >> T >> W;
35+
tree.resize(T);
36+
for(int i = 0; i < T; i++){
37+
cin >> tree[i];
38+
}
39+
// 초기화
40+
memset(dp, -1, sizeof(dp));
41+
42+
// 움직, 안 움직
43+
int move = dfs(0, W - 1, 1);
44+
int nomove = dfs(0, W, 0);
45+
46+
cout << max(move, nomove);
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)