程式設計之美-----24點遊戲
問題:
給玩家4張牌,每張牌的面值在1-13之間,允許其中有數值相同的牌,采用加、減、乘、除四則運算,允許中間運算存在小數,并且可以使用括号,但每張牌隻能用一次。構造表達式,使其結果為24.
解法一: 具體見書分析,這裡利用了枚舉,通過遞歸解法.
- #include <iostream>
- #include <cmath>
- using namespace std;
- const double Threshold = 1E-6;
- const int CardsNumber = 4;
- const int ResultValue = 24;
- double number[CardsNumber];
- string result[CardsNumber];
- bool PointsGame(int n)
- {
- if(n == 1)
- {
- // 由于浮點數運算會有精度誤差,是以用一個很小的數1E-6來做容內插補點
- // 本書2.6節中讨論了如何将浮點數轉化為分數的問題
- if(fabs(number[0] - ResultValue) < Threshold)//結果等于24
- {
- cout << result[0] << endl;//輸出表達式
- return true;
- }
- else
- {
- return false;
- }
- }
- for(int i = 0; i < n; i++)//第一個數(計算時被兩個數結果替換)
- {
- for(int j = i + 1; j < n; j++)//第二個數(計算時候被最後一個數替換)
- {
- double a, b;//存放計算的數
- string expa, expb;//存放表達式中兩個數
- a = number[i];
- b = number[j];
- number[j] = number[n - 1];//去除第二個數
- expa = result[i];
- expb = result[j];
- result[j] = result[n - 1];//表達式去除
- result[i] = '(' + expa + '+' + expb + ')';
- number[i] = a + b;//去除第一個數
- if(PointsGame(n - 1))
- return true;
- result[i] = '(' + expa + '-' + expb + ')';
- number[i] = a - b;
- if(PointsGame(n - 1))
- return true;
- result[i] = '(' + expb + '-' + expa + ')';
- number[i] = b - a;
- if(PointsGame(n - 1))
- return true;
- result[i] = '(' + expa + '*' + expb + ')';
- number[i] = a * b;
- if(PointsGame(n - 1))
- return true;
- if(b != 0)
- {
- result[i] = '(' + expa + '/' + expb + ')';
- number[i] = a / b;
- if(PointsGame(n - 1))
- return true;
- }
- if(a != 0)
- {
- result[i] = '(' + expb + '/' + expa + ')';
- number[i] = b / a;
- if(PointsGame(n - 1))
- return true;
- }
- number[i] = a;//将本次循環的結果消除,繼續測試下一對數
- number[j] = b;
- result[i] = expa;
- result[j] = expb;
- }
- }
- return false;
- }
- int main()
- {
- int x;
- for(int i = 0; i < CardsNumber; i++)
- {
- char buffer[20];
- cout << "the " << i << "th number:";
- cin >> x;
- number[i] = x;
- itoa(x, buffer, 10);
- result[i] = buffer;
- }
- if(PointsGame(CardsNumber))
- {
- cout << "Success." << endl;
- }
- else
- {
- cout << "Fail." << endl;
- }
- }
解法二:分支界限法
- #include <iostream>
- #include <set>
- #include <string>
- #include <cmath>
- using namespace std;
- #define N 4 // 4張牌,可變
- #define RES 24 // 運算結果為24,可變
- #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; // 運算出的資料
- string info; // 運算的過程
- bool operator<(const Elem& e) const
- {
- return res < e.res; // 在set的紅黑樹插入操作中需要用到比較操作
- }
- };
- int A[N]; // 記錄N個資料
- // 用二進制數來表示集合和子集的概念,0110表示集合包含第2,3個數
- set<Elem> vset[1<<N]; // 包含4個元素的集合共有16個子集0-15
- set<Elem>& Fork(int m)
- {
- // memo遞歸
- 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;
- // 得到兩個子集合的笛卡爾積,并對結果集合的元素對進行6種運算
- 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];
- // 遞歸的結束條件
- 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);//開始1111 表示四個數
- // 顯示算出24點的運算過程
- 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;
- }
- }
參考來源: 1. <<程式設計之美>> 2. http://blog.csdn.net/tianshuai11/article/details/7713640