-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP1464 Function.cpp
More file actions
61 lines (49 loc) · 1.42 KB
/
Copy pathP1464 Function.cpp
File metadata and controls
61 lines (49 loc) · 1.42 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//注意看一些规范化的命名方式
//特别是if (computed[a][b][c]) line25之后对代码的简化
//学习相关的命名习惯
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long memo[21][21][21];
//用memo来记忆结果
bool computed[21][21][21];
long long w(long long a, long long b, long long c) {
// 条件1:如果任一参数≤0,返回1
if (a <= 0 || b <= 0 || c <= 0) {
return 1;
}
// 条件2:如果任一参数>20,转换为w(20,20,20)
if (a > 20 || b > 20 || c > 20) {
return w(20, 20, 20);
}
// 检查是否已计算过
if (computed[a][b][c]) {
return memo[a][b][c];
}
// 标记为已计算
computed[a][b][c] = true;
// 条件3:a < b < c
if (a < b && b < c) {
memo[a][b][c] = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c);
}
// 条件4:其他情况
else {
memo[a][b][c] = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
}
return memo[a][b][c];
}
int main() {
// 初始化记忆化数组
memset(computed, false, sizeof(computed));
long long a, b, c;
while (cin >> a >> b >> c) {
if (a == -1 && b == -1 && c == -1) {
break;
}
long long result = w(a, b, c);
printf("w(%lld, %lld, %lld) = %lld\n", a, b, c, result);
}
return 0;
}