Uva 10400 —Game Show Math(深搜+剪枝)

来源:互联网 发布:日本电视直播软件 编辑:程序博客网 时间:2024/06/07 21:14

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 that0 < 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

题意:将给出的p个数 按照原顺序通过加减乘除运算得出最后一个数字;

分析:看到题 想到深搜,写了,交了,超时了;看了别人的博客 发现要剪枝,唉~~~,将每一步得到的值 存到一个数组里,若下次在该曾中又出现这个结果则说明再搜下去也不会有结果,二维数组,vis[n][m];n代表层数,m代表得到的结果

代码如下:

#include<stdio.h>#include<string.h>int t,n,key,ok;int a[110],b[110],vis[110][72000];int abs(int aa){    if(aa<0)        return -aa;    return aa;}void dfs(int sum,int num){    if(sum==key&&num>=n)        {ok=1;return;}    if(num>=n||abs(sum)>32000)        return;    for(int i=1;i<=4;i++)    {        int s=320320;        b[num]=i;        if(i==1)            s=sum+a[num];        else if(i==2)            s=sum-a[num];        else if(i==3)            s=sum*a[num];        else if(i==4&&sum%a[num]==0)            s=sum/a[num];           // printf("***%d\n",s);        if(abs(s)<32000&&vis[num][s]==0)//剪枝        {           vis[num][s]=1; dfs(s,num+1);        }                if(ok)//深搜出来需判断上一步结果                    return;    }}int main(){    scanf("%d",&t);    while(t--)    {        memset(b,0,sizeof(b));        memset(vis,0,sizeof(vis));        scanf("%d",&n);        for(int i=0;i<n;i++)            scanf("%d",&a[i]);        scanf("%d",&key);        ok=0;        dfs(a[0],1);        if(ok)        {            printf("%d",a[0]);            for(int i=1;i<n;i++)            {            if(b[i]==1)                printf("+");            else if(b[i]==2)                printf("-");            else if(b[i]==3)                printf("*");            if(b[i]==4)                printf("/");            printf("%d",a[i]);            }            printf("=%d\n",key);        }        else            printf("NO EXPRESSION\n");    }    return 0;}


0 0