NYOJ43 24 Point game

来源:互联网 发布:jeesz 源码下载 编辑:程序博客网 时间:2024/05/24 08:32
描述
There is a game which is called 24 Point game.
In this game , you will be given some numbers. Your task is to find an expression which have all the given numbers and the value of the expression should be 24 .The expression mustn't have any other operator except plus,minus,multiply,divide and the brackets. 
e.g. If the numbers you are given is "3 3 8 8", you can give "8/(3-8/3)" as an answer. All the numbers should be used and the bracktes can be nested. 
Your task in this problem is only to judge whether the given numbers can be used to find a expression whose value is the given number。
输入
The input has multicases and each case contains one line
The first line of the input is an non-negative integer C(C<=100),which indicates the number of the cases.
Each line has some integers,the first integer M(0<=M<=5) is the total number of the given numbers to consist the expression,the second integers N(0<=N<=100) is the number which the value of the expression should be.
Then,the followed M integer is the given numbers. All the given numbers is non-negative and less than 100
输出
For each test-cases,output "Yes" if there is an expression which fit all the demands,otherwise output "No" instead.
样例输入
2
4 24 3 3 8 8
3 24 8 3 3
样例输出
Yes

No

暴力搜索题,列出一个保存有当前数据的结构体。将计算后所有可能出现结果全部塞到队列中,接着从队列中继续取出数据,如果队列空后仍然没有得到结果,则输出no。注意运算中的除法和减法需要分出两种情况。

 /*nyoj43 唐小晨000 1016-10-30*/#include<iostream>#include<cmath>#include<queue>using namespace std;float sum;struct data{float d[5];int size;}; //d为当前状态的数据,size表示存储的数据数void memset(bool*arr,bool value,int size){for(int i=0;i<size;i++){arr[i]=value;}}//没看懂。。应该不需要//bfs暴力搜索,蓝桥桥出题的最爱bool fun(float*a,int n){queue<data>q;data cur;cur.size=n;for(int i=0;i<n;i++)cur.d[i]=a[i];//成功得到结果则搜索结束if(cur.size==2){if(fabs(cur.d[0]+cur.d[1]-sum)<1e-3)return true;if(fabs(cur.d[0]*cur.d[1]-sum)<1e-3)return true;if(fabs(cur.d[0]-cur.d[1]-sum)<1e-3)return true;if(fabs(cur.d[0])>1e-3)if(fabs(cur.d[1]/cur.d[0]-sum)<1e-3)return true;if(fabs(cur.d[1])>1e-3)if(fabs(cur.d[0]/cur.d[1]-sum)<1e-3)return true;}q.push(cur);while(!q.empty()){cur=q.front();q.pop();bool is[5];//用于记录下标位置数是否已遍历过memset(is,true,cur.size);for(int i=0;i<cur.size;i++){is[i]=false;for(int j=i+1;j<cur.size;j++){is[j]=false;data next;next.size=0;for(int k=0;k<cur.size;k++)if(is[k])next.d[next.size++]=cur.d[k];//仅将之前的运算操作中没有使用过的数记录进next中next.size++;float res[6];int len=0;float r=cur.d[i]+cur.d[j];res[len++]=r;r=cur.d[i]-cur.d[j];res[len++]=r;res[len++]=-r;//分别加入r=cur.d[i]*cur.d[j];res[len++]=r;if(fabs(next.d[j])>1e-6){r=cur.d[i]/cur.d[j];res[len++]=r;}if(fabs(next.d[i])>1e-6){r=cur.d[j]/cur.d[i];res[len++]=r;}if(next.size==2){for(int z=0;z<len;z++){if(fabs(res[z]+next.d[0]-sum)<1e-3)return true;if(fabs(res[z]*next.d[0]-sum)<1e-3)return true;if(fabs(res[z]-next.d[0]-sum)<1e-3)return true;if(fabs(res[z])>1e-3)if(fabs(next.d[0]/res[z]-sum)<1e-3)return true;if(fabs(next.d[0])>1e-3)if(fabs(res[z]/next.d[0]-sum)<1e-3)return true;}}//if(next.size==1){//for(int z=0;z<len;z++)//if(fabs(res[z]-sum)<1e-3)//return true;//}else{for(int z=0;z<len;z++){next.d[next.size-1]=res[z];q.push(next);}}is[j]=true;}is[i]=true;}}return false;}int main(){int m;cin>>m;for(int x=0;x<m;x++){int n;float a[5];cin>>n>>sum;for(int y=0;y<n;y++)cin>>a[y];if(n==1)if(fabs(a[0]-sum)<1e-3)cout<<"Yes"<<endl;elsecout<<"No"<<endl;else if(fun(a,n))cout<<"Yes"<<endl;elsecout<<"No"<<endl;}return 0;}/*13 24 3 8 8*/        


0 0