[DFS&&剪枝]uva10400 Game Show Math

Problem H
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


(Problem-setter: Sandy Graham, CS Dept, University of Waterloo)

 


題意:

在英國有一個數學遊戲,給參賽者一些正整數和一個目標數,參賽者必須在這些正整數間插入+、-、*或/ 的符號,使得最後計算的結果等於目標數。計算的方式是由左到右,而且不必管運算的優先順序(就是不管先乘除後加減那一套)。

在這個數學運算式中,有三個限制:

  • 正整數出現的次序不可改變,也就是要與輸入的順序相同
  • 因爲目標數也是一個正整數,所以在運算的過程中,你只有在可以整除的情況下纔可以使用/ 。
  • 在運算的過程中,如果你用某一​​個運算符號,會導致產生的數超出(-32000 ~ +32000)的範圍,那麼你不可以採用此運算符號。(也就是說在運算的過程中都不該有超出範圍的數出現)

思路:典型的DFS了,但是由於數據量較大,必須要剪枝,把所有的狀態記錄在vis數組裏面,記爲vis[100][64000],意思是在某一步表達式的值,遇到已經在同一步求出來的就可以認爲不必往下面搜索了。。想了半天沒想出來。。

#include<iostream>
#include<cstring>

using namespace std;

int arry[110],vis[110][64100];
int tag,res,m;
char vec[150];

void dfs(int pos,int cnt)
{
    if(tag) return;
    if(pos+1==m&&cnt==res)
    {
        tag=1;
        return;
    }
    else if(pos+1==m) return;
    if(cnt+arry[pos+1]<=32000&&cnt+arry[pos+1]>=-32000&&tag==0&&vis[pos+1][cnt+arry[pos+1]+32000]==0)
    {
		vis[pos+1][cnt+arry[pos+1]+32000]=1;
        dfs(pos+1,cnt+arry[pos+1]);
        if(tag)
        {
			vec[pos+1]='+';
            return;
        }
    }
    if(cnt-arry[pos+1]<=32000&&cnt-arry[pos+1]>=-32000&&tag==0&&vis[pos+1][cnt-arry[pos+1]+32000]==0)
    {
		vis[pos+1][cnt-arry[pos+1]+32000]=1;
        dfs(pos+1,cnt-arry[pos+1]);
        if(tag)
        {
			vec[pos+1]='-';
			return;
        }
    }

    if(cnt%arry[pos+1]==0&&cnt/arry[pos+1]<=32000&&cnt/arry[pos+1]>=-32000&&tag==0&&vis[pos+1][cnt/arry[pos+1]+32000]==0)
    {
		vis[pos+1][cnt/arry[pos+1]+32000]=1;
        dfs(pos+1,cnt/arry[pos+1]);
        if(tag)
        {
			vec[pos+1]='/';
			return;
        }
    }
	if(cnt*arry[pos+1]<=32000&&cnt*arry[pos+1]>=-32000&&tag==0&&vis[pos+1][cnt*arry[pos+1]+32000]==0)
    {
		vis[pos+1][cnt*arry[pos+1]+32000]=1;
        dfs(pos+1,cnt*arry[pos+1]);
        if(tag)
        {
			vec[pos+1]='*';
			return;
        }
    }
    return;
}

int main()
{
    int n;
    cin>>n;
    while(n--)
    {
        int i;
        tag=0;
        cin>>m;
        for(i=0;i<m;i++)
            cin>>arry[i];
        cin>>res;
		memset(vis,0,sizeof(vis));
        dfs(0,arry[0]);
        if(tag)
        {
            for(i=0;i<m-1;i++)
            {
                cout<<arry[i]<<vec[i+1];
            }
            cout<<arry[i]<<"="<<res<<endl;
        }
        else cout<<"NO EXPRESSION"<<endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章