BOJ : https://www.acmicpc.net/problem/2294
github : https://github.com/junho0956/Algorithm/blob/master/2294/2294/%EC%86%8C%EC%8A%A4.cpp
동전2 문제의 경우 2차원 dp로 해결할 수 있습니다.
지금까지 합쳐진 돈의 액수를 total로 보고 재귀로 구현해주시면 쉽게 해결할 수 있습니다.
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
32
33
34
35
36
37
|
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int dp[101][10001];
int N, won;
int arr[101];
int dfs(int nth, int total) {
if (nth > N || total > won) return 1000;
if (total == won)
return 0;
int& res = dp[nth][total];
if (res != -1)return res;
res = 1000;
for (int i = nth; i <= N; i++) {
res = min(res, dfs(i, total + arr[i]) + 1);
}
return res;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> won;
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= N; i++) cin >> arr[i];
int ans = dfs(1, 0);
if (ans == 1000) cout << "-1";
else cout << ans;
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|
'algorithm > BOJ' 카테고리의 다른 글
BOJ 1916번 최소비용 구하기 (0) | 2020.02.23 |
---|---|
BOJ 1753번 최단경로 (0) | 2020.02.23 |
BOJ 10835번 카드게임 (0) | 2020.02.21 |
BOJ 10995번 별 찍기 - 20 (0) | 2020.02.21 |
BOJ 11654번 아스키 코드 (0) | 2020.02.21 |