Poj--1950(枚举,打表)

2014-12-12 00:18:21

思路:暴力枚举运算符.....

  One line of output for each of the first 20 possible expressions -- then a line with a single integer that is the total number of possible answers. 

  这句话是坑点,就是你最多只输出20组,但你还要输出总可能数。如果直接暴搜找总可能数会T,所以打表存答案。  

 1 /*************************************************************************
 2     > File Name: 1950.cpp
 3     > Author: Natureal
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Thu 11 Dec 2014 05:58:45 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <map>
14 #include <set>
15 #include <stack>
16 #include <queue>
17 #include <iostream>
18 #include <algorithm>
19 using namespace std;
20 #define lp (p << 1)
21 #define rp (p << 1|1)
22 #define getmid(l,r) (l + (r - l) / 2)
23 #define MP(a,b) make_pair(a,b)
24 typedef long long ll;
25 const int INF = 1 << 30;
26 
27 int N;
28 int op[20];
29 ll tnum[20];
30 int top[20];
31 int cnt;
32 int ans[20];
33 
34 void Print(){
35     printf("1");
36     for(int i = 2; i <= N; ++i){
37         if(op[i - 1] == 1) printf(" + ");
38         else if(op[i - 1] == 2) printf(" - ");
39         else printf(" . ");
40         printf("%d",i);
41     }
42     puts("");
43 }
44 
45 void Dfs(int p){
46     if(cnt >= 20)
47         return;
48     if(p == N){
49         ll res;
50         int tcnt = 0;
51         for(int i = 1; i <= N; ++i){
52             ll v = i;
53             while(i < N && op[i] == 3){
54                 if(i + 1 >= 10)
55                     v = v * 100 + i + 1;
56                 else
57                     v = v * 10 + i + 1;
58                 ++i;
59             }
60             ++tcnt;
61             if(tcnt < N) top[tcnt] = op[i];
62             tnum[tcnt] = v;
63         }
64         res = tnum[1];
65         for(int i = 1; i < tcnt; ++i){
66             if(top[i] == 1) res += tnum[i + 1];
67             else res -= tnum[i + 1];
68         }
69         if(res == 0){
70             ++cnt;
71             Print();
72         }
73         return;
74     }
75     for(int i = 1; i <= 3; ++i){
76         op[p] = i;
77         Dfs(p + 1);
78     }
79 }
80 
81 int main(){
82     ans[15] = 1350;
83     ans[14] = 437;
84     ans[13] = 197;
85     ans[12] = 162;
86     ans[11] = 88;
87     ans[10] = 17;
88     scanf("%d",&N);
89     cnt = 0;
90     Dfs(1);
91     printf("%d\n",max(cnt,ans[N]));
92     return 0;
93 }

 

posted @ 2014-12-12 00:29  Naturain  阅读(125)  评论(0编辑  收藏  举报