hdu 3325 Arithmetically Challenged(dfs全排+暴力枚举破解+set集合的应用)

http://acm.hdu.edu.cn/showproblem.php?pid=3325

Arithmetically Challenged

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 331    Accepted Submission(s): 139

Problem Description
Challenge 24 is a popular mathematics game used in many grade schools. In each game, contestants are given a card with four positive integers i1, i2, i3, i4 on it, and the first one who can use all of these numbers and any combination of the four basic arithmetic operations to get 24 wins. Each of the numbers i1, i2, i3, i4 must be used exactly once. Division can be used only if the divisor evenly divides the dividend (i.e., you can perform 6/2 but not 6/4). For example, if the card contains the numbers 7, 2, 5 and 1, possible solutions are (7-2)*5-1 or (7+1)*(5-2). Hmmm . . . this sounds like a source of a good programming problem.

Write a program that determines the longest consecutive sequence of integers that can be obtained by different ways of arithmetically combining the four integers. For example, with 7, 2, 5 and 1 the longest consecutive sequence is -18 to 26 (yes, we're allowing final results to be negative). The "+" and "-" operators must be used as binary operators, not as unary signs.
Input
Each test case will consist of a single line containing the four, not necessarily distinct, positive integers, none of which will exceed 100. A line containing four 0’s will terminate input.
Output
For each test case, output the case number and the longest consecutive sequence of obtainable values, in the format shown in the sample output. If there is more than one longest consecutive sequence, use the one with the largest first value.
Sample Input
7 2 5 1
8 15 38 3
0 0 0 0
Sample Output
Case 1: -18 to 26
Case 2: 150 to 153
 
 
题解:
首先该题只有四个数,可能的全排列有4!=24种,全排列的话可以用dfs解决,解决好全排列之后就是暴力破解了,对于每次全排列结果暴力破解可以分为有6+1种情况,6种情况分别为a/b,b/a,a+b,a*b,a-b,b-a,但是这六种情况不能包括所有情况,还有1种情况为(a@b)@(c@d)这里的运算符“+-*/”用@表示
 
这里计算时间复杂度为:4!*6*6*6=5184  还是可以接受的
 
另外,我们用set集合存得到的数据,set集合会自动消除重复的数并且排好序,很好用的噢   这里用map代替也是可以的
 
下面用代码实现:
  1 #include<iostream>
  2 #include<stdio.h>
  3 #include<math.h>
  4 #include<algorithm>
  5 #include<string.h>
  6 #include<string>
  7 #include<ctime>
  8 #include<queue>
  9 #include<list>
 10 #include<map>
 11 #include<set>
 12 #define INF 999999999
 13 #define MAXN 100000
 14 using namespace std;
 15 int b[10],mark[10],a[10],arr[MAXN+1],pos;
 16 set<int> ss;
 17 
 18 //针对6种运算情况进行计算
 19 int fenjie(int a,int b,int p)
 20 {
 21     if(p==0)
 22         return a+b;
 23     else if(p==1)
 24         return a-b;
 25     else if(p==5)
 26         return a*b;
 27     else if(p==3)
 28     {
 29         if(!b||a%b!=0)
 30             return INF;    //当遇到除法则先判断除数是否为0,这是必要的,不然会RE
 31         return a/b;
 32     }
 33     else if(p==4)
 34     {
 35         if(!a||b%a!=0)
 36             return INF;    //同上
 37         return b/a;
 38     }
 39     else if(p==2)
 40         return b-a;
 41 }
 42 
 43 void baoli()
 44 {
 45     int i,j,k,temp2,sum1,sum2,sum3;
 46     for(i=0;i<6;i++)
 47     {
 48         if((sum1=fenjie(a[b[0]],a[b[1]],i))==INF)   //1.非法时候进行处理
 49             continue;
 50         for(j=0;j<6;j++)
 51         {
 52             if((sum2=fenjie(sum1,a[b[2]],j))==INF)  //2
 53                 continue;
 54             for(k=0;k<6;k++)
 55             {
 56                 if((sum3=fenjie(sum2,a[b[3]],k))!=INF)  //3
 57                     ss.insert(sum3);
 58                 //1 2 3 是对前6种可能得暴力枚举
 59                 
 60                 //最后一种(a@b)@(c@d)的处理
 61                 if((temp2=fenjie(a[b[2]],a[b[3]],k))==INF)
 62                     continue;
 63                 if((sum3=fenjie(sum1,temp2,j))==INF)
 64                     continue;
 65                 ss.insert(sum3);
 66             }
 67         }
 68     }
 69 }
 70 
 71 //dfs对数组进行全排列
 72 void dfs(int s)   
 73 {
 74     int i;
 75     if(s>=4)
 76     {
 77         baoli();
 78     }
 79     else
 80     {
 81         for(i=0;i<4;i++)
 82         {
 83             if(!mark[i])
 84             {
 85                 mark[i]=1;
 86                 b[s]=i;
 87                 dfs(s+1);
 88                 mark[i]=0;
 89             }
 90         }
 91     }
 92 }
 93 int main()
 94 {
 95     int sb=1;
 96     while(~scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]))
 97     {
 98         if(a[0]+a[1]+a[2]+a[3]==0)
 99             break;
100         int i;
101         memset(mark,0,sizeof(mark));
102         pos=0;
103         ss.clear();
104         dfs(0);
105         while(!ss.empty())
106         {
107             arr[pos++]= * ss.begin();
108             ss.erase(ss.begin());
109         }
110         int maks=0,cnt=0,ed=0;
111         for(i=1;i<pos;i++)
112         {
113             if(arr[i]==arr[i-1]+1)
114                 cnt++;
115             else
116                 cnt=0;
117             if(maks<=cnt)
118             {
119                 maks=cnt;
120                 ed=arr[i];
121             }
122         }
123         printf("Case %d: %d to %d\n",sb++,ed-maks,ed);
124     }
125     return 0;
126 }
 
 
 
原文地址:https://www.cnblogs.com/crazyapple/p/3008726.html