The beauty of programming 1.16-24 o'clock games

Source: Internet
Author: User
Tags repetition

Problem:
Four cards are given to the player. Each card has a face value between 1 and 13. cards with the same value are allowed. The plus, minus, multiplication, and Division operations allow decimal places in the intermediate operation, brackets can be used, but each card can only be used once. Construct an expression to make the result 24.
Solution:
The traditional enumeration method produces a large number of repeated operations, mainly including two types of repetition: Repetition of calculation results and repetition of arrangement. Assume that the four cards are 3, 3, 8, and 8. We perform an operation on 3 (6 operations) to obtain 6 0, 0, 1, and 9, the duplicate data is the same as the calculation result, and the set is not repetitive. The enumeration algorithm sorts cards in order during enumeration. As cards can be repeated, a large number of duplicates are generated (3) 8 8, (3 8) 3 8, (3 8) 3 8, (3 8) 3 8, (3 8) 3 8, (8 8) 3 3, which are arranged repeatedly, use the divide and conquer method and memo to solve the problem. The binary number is used to express the concept of a set and a subset. We can use a number to indicate the elements in a subset, and use this number as an index to find the result set generated after the set operation.
[Cpp]
# Include <iostream>
# Include <set>
# Include <string>
Using namespace std;
 
# Define N 4 // 4 cards, variable
# Define RES 24 // The calculation result is 24, variable
# Define EPS 1e-6
 
Struct Elem
{
Elem (double r, string & I): res (r), info (I ){}
Elem (double r, char * I): res (r), info (I ){}
Double res; // the calculated data.
String info; // operation process
Bool operator <(const Elem & e) const
{
Return res <e. res; // A comparison operation is required in the insert operation of the set red/black tree.
}
};
 
Int A [N]; // records N data records
// The binary number is used to represent the concept of a set and a subset. The value 0110 indicates that the set contains the second and second numbers.
Set <Elem> vset [1 <N]; // a set of 4 elements has 16 subsets, ranging from 0 to 15.
 
Set <Elem> & Fork (int m)
{
// Memo Recursion
If (vset [m]. size ())
{
Return vset [m];
}
For (int I = 1; I <= m/2; I ++)
If (I & m) = I)
{
Set <Elem> & s1 = Fork (I );
Set <Elem> & s2 = Fork (m-I );
Set <Elem >:: iterator cit1;
Set <Elem >:: iterator cit2;
// Obtain the Cartesian product of two subsets and perform six operations on the element pairs of the result set
For (cit1 = s1.begin (); cit1! = S1.end (); cit1 ++)
For (cit2 = s2.begin (); cit2! = S2.end (); cit2 ++)
{
String str;
Str = "(" + cit1-> info + "+" + cit2-> info + ")";
Vset [m]. insert (Elem (cit1-> res + cit2-> res, str ));
Str = "(" + cit1-> info + "-" + cit2-> info + ")";
Vset [m]. insert (Elem (cit1-> res-cit2-> res, str ));
Str = "(" + cit2-> info + "-" + cit1-> info + ")";;
Vset [m]. insert (Elem (cit2-> res-cit1-> res, str ));
Str = "(" + cit1-> info + "*" + cit2-> info + ")";
Vset [m]. insert (Elem (cit1-> res * cit2-> res, str ));
If (abs (cit2-> res)> EPS)
{
Str = "(" + cit1-> info + "/" + cit2-> info + ")";
Vset [m]. insert (Elem (cit1-> res/cit2-> res, str ));
}
If (abs (cit1-> res)> EPS)
{
Str = "(" + cit2-> info + "/" + cit1-> info + ")";
Vset [m]. insert (Elem (cit2-> res/cit1-> res, str ));
}
}
}
Return vset [m];
}
 
Int main ()
{
Int I;
For (I = 0; I <N; I ++)
Cin> A [I];
// Recursive end Condition
For (I = 0; I <N; I ++)
{
Char str [10];
Sprintf (str, "% d", A [I]);
Vset [1 <I]. insert (Elem (A [I], str ));
}
Fork (1 <N)-1 );
// Display the 24-point calculation process
Set <Elem >:: iterator it;
For (it = vset [(1 <N)-1]. begin ();
It! = Vset [(1 <N)-1]. end (); it ++)
{
If (abs (it-> res-RES) <EPS)
Cout <it-> info <endl;
}
}

Although the time complexity of the above algorithms is less than the exhaustive method, because there are only four cards in the 24-point game, the computing scale is very small, and the above algorithm introduces the set data structure, the bottom layer of the data structure is a red/black tree, which consumes a lot of time and the access complexity O (h) is greater than O (1) of enumeration. Therefore, in actual operation, it is faster than the enumeration method. The following is an enumeration algorithm written in the book, which is faster than the actual operation:
[Cpp]
# Include <iostream>
# Include <string>
# Include <cstdlib>
# Include <ctime>
Using namespace std;
 
Const double EPS = 1e-6;
Const int NUM = 4;
Const int RES = 24;
 
Double A [NUM];
String res_str [NUM];
 
Int times = 0;
 
Bool process (int n)
{
// Exit condition
If (n = 1)
{
If (abs (A [0]-RES) <EPS)
{
Cout <res_str [0] <endl;
Return true;
}
Else
Return false;
}
Double a, B;
String expa, expb;
For (int I = 0; I <n; I ++)
For (int j = I + 1; j <n; j ++)
{
Times ++;
// Save status (operand I, j)
A = A [I];
B = A [j];
Expa = res_str [I];
Expb = res_str [j];
 
// Change the status
A [j] = A [n-1];
Res_str [j] = res_str [n-1];
A [I] = a + B;
Res_str [I] = '(' + expa + '+ expb + ')';
If (process (n-1 ))
Return true;
A [I] = a-B;
Res_str [I] = '(' + expa + '-' + expb + ')';
If (process (n-1 ))
Return true;
A [I] = B-;
Res_str [I] = '(' + expb + '-' + expa + ')';
If (process (n-1 ))
Return true;
A [I] = a * B;
Res_str [I] = '(' + expa + '*' + expb + ')';
If (process (n-1 ))
Return true;
If (B! = 0)
{
A [I] = a/B;
Res_str [I] = '(' + expa + '/' + expb + ')';
If (process (n-1 ))
Return true;
}
If (! = 0)
{
A [I] = B/;
Res_str [I] = '(' + expb + '/' + expa + ')';
If (process (n-1 ))
Return true;
}
 
// Restore status
A [I] =;
A [j] = B;
Res_str [I] = expa;
Res_str [j] = expb;
}
Return false;
}
 
 
Int main ()
{
For (int I = 0; I <NUM; I ++)
{
Cin> A [I];
Char c [10];
Sprintf (c, "%. 0f", A [I]);
Res_str [I] = c;
}
Clock_t start = clock ();
If (process (NUM ))
Cout <res_str [0] <endl;
Else
Cout <"failed" <endl;
Clock_t duration = clock ()-start;
Cout <duration <endl;
}


 
[Cpp]


Author: linyunzju

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.