Link:
Http://uva.onlinejudge.org/index.php? Option = com_onlinejudge & Itemid = 8 & category = 113 & page = show_problem & problem = 1341
Type: backtracking
Original question:
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 wrong times. the expression shoshould 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 contains 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 casesN.
Each subsequent line contains the number of positive numbers in the sequenceP, FollowedPPositive numbers, followed by the target number. Note that0 <p £0. 100. There may be duplicate numbers in
Sequence. But all the numbers are less32000.
Output
The output file shoshould contain an expression, including allKNumbers 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 2000
5 12 2 5 1 2 4
Sample output
5 + 7/4 = 3
No expression
12-2/5*1*2 = 4
Question:
You can use the +,-, *, And/symbols to calculate a number and a target number (here, the four symbols have the same priority ), is the result of these numeric expressions as the target number.
Analysis and Summary:
In this case, the question is most suitable for backtracking, but the data size of the question is estimated to have scared many people to use backtracking. Use backtracking 0.084s water...
Code:
/* * UVa: 10400 - Game Show Math * Type: dfs * Time: 0.084s * Author: D_Double * */#include<iostream>#include<cstring>#include<cstdio>#define ADD 32000using namespace std;int arr[120], target, p;char route[120];bool flag, vis[102][32002*2+2];inline bool isOk(int m, int cur){ return m>=-32000&&m<=32000 && !vis[cur][m+ADD];}void dfs(int cur, int sum){ if(flag)return ; if(cur==p){ if(sum==target) flag=true; return; } if(flag) return; for(int i=0; i<4; ++i){ if(i==0&&isOk(sum+arr[cur], cur)){ vis[cur][sum+arr[cur]+ADD] = true; route[cur-1]='+'; dfs(cur+1, sum+arr[cur]); } else if(i==1 && isOk(sum-arr[cur], cur)){ vis[cur][sum-arr[cur]+ADD] = true; route[cur-1]='-'; dfs(cur+1,sum-arr[cur]); } else if(i==2 && isOk(sum*arr[cur], cur)){ vis[cur][sum*arr[cur]+ADD] = true; route[cur-1]='*'; dfs(cur+1,sum*arr[cur]); } else if(i==3 && arr[cur]!=0 && isOk(sum/arr[cur], cur)){ vis[cur][sum/arr[cur]+ADD] = true; route[cur-1]='/'; dfs(cur+1,sum/arr[cur]); } if(flag)return; }}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&p); for(int i=0; i<p; ++i) scanf("%d",&arr[i]); scanf("%d",&target); memset(vis, false, sizeof(vis)); vis[0][arr[0]+ADD] = true; flag=false; dfs(1, arr[0]); if(flag){ printf("%d",arr[0]); for(int i=1; i<p; ++i){ printf("%c%d",route[i-1],arr[i]); } printf("=%d\n",target); } else printf("NO EXPRESSION\n"); } return 0;}
-- The meaning of life is to give it meaning.
Original
Http://blog.csdn.net/shuangde800
, By d_double (reprinted, please mark)