【HDOJ】3419 The Three Groups

记忆化搜索。

 1 /* 3419 */
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 
 6 #define MAXN 10
 7 
 8 int ans;
 9 int n[3];
10 int x[3];
11 bool visit[MAXN];
12 int cnt[MAXN][MAXN][MAXN];
13 
14 void dfs(int d, int i, int v) {
15     if (i == 3) {
16         if (x[0]*x[1] == x[2])
17             ++ans;
18         return ;
19     }
20     if (d < n[i]) {
21         for (int j=1; j<MAXN; ++j) {
22             if (!visit[j]) {
23                 visit[j] = true;
24                 dfs(d+1, i, v*10+j);
25                 visit[j] = false;
26             }
27         }
28     } else {
29         x[i] = v;
30         dfs(0, i+1, 0);
31     }
32 }
33 
34 int main() {
35     int a, b, c;
36     int i, j, k;
37     
38     #ifndef ONLINE_JUDGE
39         freopen("data.in", "r", stdin);
40         freopen("data.out", "w", stdout);
41     #endif
42     
43     memset(cnt, -1, sizeof(cnt));
44     while (scanf("%d %d %d", &a, &b, &c)!=EOF && (a||b||c)) {
45         if (a+b < c) {
46             puts("0");
47         } else if (a>c || b>c){
48             puts("0");
49         } else if (a+b-c>1) {
50             puts("0");
51         } else if (cnt[a][b][c] >= 0) {
52             printf("%d
", cnt[a][b][c]);
53         } else {
54             memset(visit, false, sizeof(visit));
55             n[0] = a;
56             n[1] = b;
57             n[2] = c;
58             ans = 0;
59             dfs(0, 0, 0);
60             cnt[a][b][c] = ans;
61             printf("%d
", ans);
62         }
63     }
64     
65     return 0;
66 }
原文地址:https://www.cnblogs.com/bombe1013/p/4305069.html