[sicily online]1050. Numbers & Letters

来源:互联网 发布:云计算的概念 编辑:程序博客网 时间:2024/05/20 23:02

Constraints

Time Limit: 3 secs, Memory Limit: 32 MB

Description

In the early 80’s, a popular TV show on Dutch television was ‘Cijfers en Letters’ (Numbers and Letters). This game consisted of two game elements, in which the main goal was to outclass your opponent. Letters is a game in which you are given a number of letters with which you should form the longest Dutch word possible. Since Dutch is a very hard language to learn we will postpone implementation of this game element until after the contest. 
For the second game element ‘Numbers’ 5 different numbers are chosen, together with a target number. The aim is to use some arithmetic on (some of) the five numbers to form the target number. Each number can be used only once. It might not be possible to form the target number given the input numbers, in that case the largest number smaller than the target number that can be calculated should be given. The only mathematical operations allowed are: +, -, *, /.  All intermediate results should be integers, so division is not always allowed (e.g. (2*2)/4 is OK, but 2*(2/4) is not). 
Examples: 
- If the 5 numbers are 1, 2, 3, 7 and 100 and the target number is 573, the target number can be reached as follows: (((100-1)*2)-7)*3. -If the 5 numbers are 3, 26, 78, 12 and 17, and the target number is 30, the target number can be reached as follows: (78*3)-(12*17). 
- If the 5 numbers are 67, 69, 58, 22, 2, and the target number is 929, the target number cannot be reached, but the largest number smaller than the target number that can be reached is 923 = (22-(67-58))*(69+2). 
Your assignment is to write a program that calculates the best approximation from below of the target number using arithmetic on the 5 given numbers. Note that if it is not possible to reach the exact number, you should give the largest reachable number below the target number.

Input

The first line contains the number of runs, N. The next N lines consist of six numbers separated by a space. The first 5 numbers Mi, 1≤Mi≤100, are the numbers you can use to calculate the target number. The sixth number is the target number T, 0≤T≤1000.

Output

The output consists of N rows, each containing the best approximation of the target number using the 5 given numbers.

Sample Input

31 2 3 7 100 5733 26 78 12 17 3067 69 58 22 2 929

Sample Output

57330 923

题目分析:

变化的24点问题,5个数可能没有全部用到。因为只要找到了目标节点就停止,所以总体性能。dfs比bfs要好。所以这个题目就是用dfs来遍历所有情况

1,刚开始我是用栈来实现dfs,结果一直TLE

2,后来改成用函数递归来实现dfs,ac了

后来做了下比较,用系统的函数递归比用栈快了将近5倍的时间。

/**/#include<iostream>#include <iomanip>#include<stdio.h>#include<cmath>#include<iomanip>#include<list>#include <map>#include <vector>#include <string>#include <algorithm>#include <sstream>#include <stack>#include<queue>#include<string.h>#include<set>using namespace std;int Max,goal;bool dfs(vector<long long> &to){for(int i=0;i<to.size();i++){if(to[i]==goal){return true;}else if(to[i]<goal&&to[i]>Max){Max=to[i];}}for(int i=0;i<to.size();i++){for(int j=i+1;j<to.size();j++){vector<long long> baseTmp;for(int k=0;k<to.size();k++){if(k!=i&&k!=j)baseTmp.push_back(to[k]);}vector<long long> plusTmp(baseTmp);plusTmp.push_back(to[i]+to[j]);if(dfs(plusTmp))return true;vector<long long> minusTmp1(baseTmp);minusTmp1.push_back(to[i]-to[j]);if(dfs(minusTmp1))return true;vector<long long> minusTmp2(baseTmp);minusTmp2.push_back(to[j]-to[i]);if(dfs(minusTmp2))return true;vector<long long> multiplyTmp(baseTmp);multiplyTmp.push_back(to[i]*to[j]);if(dfs(multiplyTmp))return true;if(to[i]==0||to[j]==0)continue;if(max(to[i],to[j])%min(to[i],to[j])==0){vector<long long> divideTmp(baseTmp);divideTmp.push_back(max(to[i],to[j])/min(to[i],to[j]));if(dfs(divideTmp))return true;}}}return false;}int main(){int n;//cin>>n;scanf("%d",&n);for(int xx=0;xx<n;xx++){vector<long long> num(5);goal;for(int i=0;i<5;i++){//cin>>num[i];scanf("%d",&num[i]);}//cin>>goal;scanf("%d",&goal);Max=-9999;clock_t start=clock();if(dfs(num))printf("%d\n",goal);else printf("%d\n",Max);clock_t end=clock();double Total_time = (double)(end-start) / CLOCKS_PER_SEC; //printf( "%f seconds/n", Total_time); }}//自己用栈来实现深度优先搜索//int main()//{//int n;////cin>>n;//scanf("%d",&n);//for(int xx=0;xx<n;xx++)//{//int num[5];//int goal;//for(int i=0;i<5;i++)//{////cin>>num[i];//scanf("%d",num+i);//}////cin>>goal;//scanf("%d",&goal);//int Max=-9999;//clock_t start=clock();//bool flag=false;//stack< vector<int> >st;//vector<int> init(num,num+5);//st.push(init);//while(!st.empty())//{//vector<int> to=st.top();//st.pop();//for(int i=0;i<to.size();i++)//{//if(to[i]==goal)//{//flag=true;//break;//}//else if(to[i]<goal&&to[i]>Max)//{//Max=to[i];//}//}//for(int i=0;i<to.size();i++)//{//for(int j=i+1;j<to.size();j++)//{//vector<int> baseTmp;//for(int k=0;k<to.size();k++)//{//if(k!=i&&k!=j)//baseTmp.push_back(to[k]);//}////vector<int> plusTmp(baseTmp);//plusTmp.push_back(to[i]+to[j]);//st.push(plusTmp);////vector<int> minusTmp1(baseTmp);//minusTmp1.push_back(to[i]-to[j]);//st.push(minusTmp1);////vector<int> minusTmp2(baseTmp);//minusTmp2.push_back(to[j]-to[i]);//st.push(minusTmp2);////vector<int> multiplyTmp(baseTmp);//multiplyTmp.push_back(to[i]*to[j]);//st.push(multiplyTmp);////if(to[i]==0||to[j]==0)//continue;//if(max(to[i],to[j])%min(to[i],to[j])==0)//{//vector<int> divideTmp(baseTmp);//divideTmp.push_back(max(to[i],to[j])/min(to[i],to[j]));//st.push(divideTmp);//}//}//}//}//if(flag)//printf("%d\n",goal);//else printf("%d\n",Max);//clock_t end=clock();//double Total_time = (double)(end-start) / CLOCKS_PER_SEC; //printf( "%f seconds/n", Total_time); //}//}