algorithm/BOJ
BOJ 9184번 신나는 함수 실행
_JunHo
2020. 2. 13. 16:19
BOJ : https://www.acmicpc.net/problem/9184
github : https://github.com/junho0956/Algorithm/blob/master/9184/9184/%EC%86%8C%EC%8A%A4.cpp
dp문제라기 보다는 꼼꼼함을 따지는? 문제였습니다
dp의 크기를 잡는 힌트는 문제에 주어져있고
처음에 그 범위는 읽었지만, 바로 수정한 다음에 함수호출을 하여서 w(a,b,c) 를 할때 틀렸네요,,
이것만 주의하시면 될 것 같습니다.
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
|
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
ll dp[22][22][22];
ll solve(int a, int b, int c) {
if (a <= 0 || b <= 0 || c <= 0) return 1;
ll& res = dp[a][b][c];
if (res) return res;
if (a > 20 || b > 20 || c > 20) return res = solve(20, 20, 20);
if (a < b && b < c) return res = solve(a, b, c - 1) + solve(a, b - 1, c - 1) - solve(a, b - 1, c);
else return res = solve(a - 1, b, c) + solve(a - 1, b - 1, c) + solve(a - 1, b, c - 1) - solve(a - 1, b - 1, c - 1);
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int a, b, c;
while (1) {
cin >> a >> b >> c;
if (a == -1 && b == -1 && c == -1) break;
cout << "w(" << a << ", " << b << ", " << c << ") = ";
if (a > 20) a = 21;
if (b > 20) b = 21;
if (c > 20) c = 21;
cout << solve(a, b, c) << "\n";
memset(dp, 0, sizeof(dp));
}
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
|