您的位置:首页 > 其它

Game Show Math +uva+dfs+剪枝

2014-07-17 21:08 393 查看
Game Show Math

Input:
standard input

Output: standard output

Time Limit: 15 seconds

A game show in Britain has a segment where it gives its contestants a sequence of positive numbers and a target number. The contestant must make a mathematical expression using all of the numbers in the sequence and only the operators: +, -, *,
and, /. Each number in the sequence must be used exactly once, but each operator may be used zero to many times. The expression should be read from left to right, without regard for order of operations, to calculate the target number. It is
possible that no expression can generate the target number. It is possible that many expressions can generate the target number.

There are three restrictions on the composition of the mathematical expression:

o the numbers in the expression must appear in the same order as they appear in the input file

o since the target will always be an integer value (a positive number), you are only allowed to use / in the expression when the result will give a remainder of zero.

o you are only allowed to use an operator in the expression, if its result after applying that operator is an integer from (-32000 .. +32000).

Input

The input file describes multiple test cases. The first line contains the number of test cases n.

Each subsequent line contains the number of positive numbers in the sequence p, followed by p positive numbers, followed by the target number. Note that 0 < p £ 100. There may be duplicate numbers in the
sequence. But all the numbers are less than 32000.

Output

The output file should contain an expression, including all k numbers and (k-1) operators plus the equals sign and the target. Do not include spaces in your expression. Remember that order of operations does not apply here.If
there is no expression possible output "NO EXPRESSION" (without the quotes). If more than one expression is possible, any one of them will do.

Sample Input

3

3 5 7 4 3

2 1 1 2000

5 12 2 5 1 2 4

Sample Output

5+7/4=3

NO EXPRESSION

12-2/5*1*2=4

解决方案:有20s的时间,但直接暴力还是不够滴,要dfs+剪枝,剪枝我想半天想不出个所以然来。它是靠标记每层的结果来实现剪枝滴,当这一层的这一个结果的达不到最终目的的时候,标志下来了,这一部分就被剪掉了,当遇到这一层这样的结果时,就不搜下去。

代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define MMAX 64004
using namespace std;
int num[101],ans;
int Cur[101];
bool vis[101][MMAX];
int f;
int n;
void dfs(int cur,int sum){
if(cur>=n&&sum==ans){f=1;return ;}
if(f||(cur>=n&&sum!=ans)||cur>=n) return;
for(int i=0;i<4;i++){
int s=32001;
//cout<<num[cur]<<endl;
if(i==0) s=sum+num[cur];
else if(i==1) s=sum-num[cur];
else if(i==2) s=sum*num[cur];
else if(sum%num[cur]==0&&i==3)
{s=sum/num[cur];}
if(!f&&abs(s)<=32000&&!vis[cur][32000+s]){///这里有个小细节,当除法的时候,若不能整除,则此处剪枝
vis[cur][32000+s]=true;
Cur[cur]=i;
//cout<<i<<endl;
dfs(cur+1,s);
}
if(f) return ;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&num[i]);
}

f=0;
memset(vis,false,sizeof(vis));
//   memset(cur,0,s)
scanf("%d",&ans);
dfs(1,num[0]);
if(f){
for(int i=0;i<n;i++){

if(i!=0) {
if(Cur[i]==0){printf("+");}
if(Cur[i]==1){printf("-");}
if(Cur[i]==2){printf("*");}
if(Cur[i]==3){printf("/");}

}
printf("%d",num[i]);
}
printf("=%d\n",ans);
}
else {printf("NO EXPRESSION\n");}
}

return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: