算m点问题,深搜解决

算法导论课后的一道题。

题目:给定k个正整数,用算数运算符+,-,*,/将这k个正整数链接起来,使最终的得数恰为m。

样例输入:

5 125

7 2 2 12 3

样例输出:

7 3 = 21; 21 12 = 252 ; 252 - 2 = 250; 250 / 2 = 125

简单的用深搜解决了,不考虑优化什么的了,数据多的话会超时,不过这不是A题就这样吧~XD

代码:

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <map>
#include <queue>
#include <ctime>
using namespace std;
#define maxn 100
int data[maxn],vis[maxn],num[maxn];
char op[maxn];
int n,m;
int value = 0;
int flag = 0;
void output() {
int tmp = num[0];
for(int i = 1; i < n ; i++) {
cout<<tmp<<" ";
if(op[i] == '+') {
cout<<"+"<<" ";
cout<<num[i]<<" = ";
tmp += num[i];
cout<<tmp<<";";
}
if(op[i] == '-') {
cout<<"-"<<" ";
cout<<num[i]<<" = ";
tmp -= num[i];
cout<<tmp<<";";
}
if(op[i] == '*') {
cout<<"*"<<" ";
cout<<num[i]<<" = ";
tmp *= num[i];
cout<<tmp<<";";
}
if(op[i] == '/') {
cout<<"/"<<" ";
cout<<num[i]<<" = ";
tmp /= num[i];
cout<<tmp<<";";
}
}
}
void dfs(int t) {
//cout<<t<<" "<<value<<" "<<endl;
if(t >= n) {
if(value == m) {
cout<<"found!"<<endl;
output();
flag = 1;
}
}
else {
for(int i = 0; i < n ; i++) {
if(t == 0) {
if(!vis[i]) {
value = data[i];
num[t] = data[i];
vis[i] = 1;
dfs(t + 1);
vis[i] = 0;
}
}
else {
if(!vis[i]) {
vis[i] = 1;
int tmp = value;
op[t] = '+';
num[t] = data[i];
value = tmp + data[i];
dfs(t + 1);
if(flag) return;
op[t] = '-';
num[t] = data[i];
value = tmp - data[i];
dfs(t + 1);
if(flag) return;
op[t] = '*';
num[t] = data[i];
value = tmp * data[i];
dfs(t + 1);
if(flag) return ;
if(data[i] != 0) {
op[t] = '/';
num[t] = data[i];
value = tmp / data[i];
dfs(t + 1);
if(flag) return;
}
value = tmp;
vis[i] = 0;
}
}
}
}
}
int main() {
// freopen("in","r",stdin);
cin>>n>>m;
for(int i = 0; i < n; i++)
cin>>data[i];
memset(vis, 0, sizeof(vis));
memset(num, 0, sizeof(num));
memset(op, 0, sizeof(op));
dfs(0);
if(!flag) cout<<"not found!"<<endl;
}