24点算法实现

  1. #include <fstream>
  2. #include <algorithm>
  3. #include <string>
  4. #include <sstream>
  5. #include <list>
  6. #include <cmath>
  7. #include <climits>
  8. #include <bitset>
  9. using namespace std;
  10. const char* INPUT_FILE  = "game.in";
  11. const char* OUTPUT_FILE = "game.out";
  12. const int NUMBER_COUNT  = 6;
  13. const int STATE_COUNT   = (1 << NUMBER_COUNT);
  14. const int MAX_NUMBER    = 100;
  15. const int MAX_EXPECTION = 1000;
  16. const int MAX_VALUE             = MAX_EXPECTION * MAX_NUMBER;
  17. struct Node {
  18.         int value;
  19.         int left, right;        
  20.         int leftvalue, rightvalue;
  21.         char opr;
  22. };
  23. typedef list<Node> NodeList;
  24. struct State {
  25.         bitset<MAX_VALUE+10> exist;
  26.         NodeList nodelist;
  27. };
  28. int number[NUMBER_COUNT], expection;
  29. State state[STATE_COUNT];
  30. void ReadData()
  31. {
  32.         ifstream fin(INPUT_FILE);
  33.         
  34.         for (int i = 0; i < NUMBER_COUNT; i++) {
  35.                 fin >> number[i];
  36.         }
  37.         fin >> expection;
  38. }
  39. void Init()
  40. {
  41.         Node node ;
  42.         for (int i = 0; i < NUMBER_COUNT; i++) {
  43.                 node.value              = number[i];
  44.                 node.left = node.right = -1;
  45.                 state[(1 << i)].nodelist.push_back(node);
  46.                 state[(1 << i)].exist[node.value] = true;
  47.         }
  48. }
  49. void Merge(int a, int b, int x)
  50. {      
  51.         Node node;      
  52.         NodeList::const_iterator i, j;
  53.         for (i = state[a].nodelist.begin(); i != state[a].nodelist.end();
  54. i++) {
  55.                 for (j = state[b].nodelist.begin(); j != state[b].nodelist.en
  56. d(); j++)
  57. {                                      
  58.                         node.value = (*i).value + (*j).value;
  59.                         node.left  = a;
  60.                         node.right = b;                 
  61.                         node.leftvalue  = (*i).value;
  62.                         node.rightvalue = (*j).value;
  63.                         node.opr   = '+';
  64.                         if ( (node.value <= MAX_VALUE) && (!state[x].exist[no
  65. de.value]) ) {
  66.                                 state[x].nodelist.push_back(node);
  67.                                 state[x].exist[node.value] = true;
  68.                         }
  69.                         /
  70.                         double tmp = double((*i).value) * double((*j).value);
  71.                         if (tmp < INT_MAX) {
  72.                                 node.value = (*i).value * (*j).value;
  73.                                 node.left  = a;
  74.                                 node.right = b;
  75.                                 node.leftvalue  = (*i).value;
  76.                                 node.rightvalue = (*j).value;
  77.                                 node.opr   = '*';
  78.                                 if ( (node.value <= MAX_VALUE) && (!state[x].
  79. exist[node.value]) )
  80. {
  81.                                         state[x].nodelist.push_back(node);
  82.                                         state[x].exist[node.value] = true;
  83.                                 }
  84.                         }
  85.                         /
  86.                         if ((*i).value >= (*j).value) {
  87.                                 node.value = (*i).value - (*j).value;
  88.                                 node.left  = a;
  89.                                 node.right = b;
  90.                                 node.leftvalue  = (*i).value;
  91.                                 node.rightvalue = (*j).value;
  92.                                 node.opr   = '-';
  93.                         } else {
  94.                                 node.value = (*j).value - (*i).value;
  95.                                 node.left  = b;
  96.                                 node.right = a;
  97.                                 node.leftvalue  = (*j).value;
  98.                                 node.rightvalue = (*i).value;
  99.                                 node.opr   = '-';
  100.                         }
  101.                                                 
  102.                         if ( (node.value <= MAX_VALUE) && (!state[x].exist[no
  103. de.value]) ) {
  104.                                 state[x].nodelist.push_back(node);
  105.                                 state[x].exist[node.value] = true;
  106.                         }
  107.                         /
  108.                         if ( ((*j).value != 0) && ((*i).value >= (*j).value) &
  109. &
  110.                                         ((*i).value % (*j).value == 0) )
  111.                         {
  112.                                 node.value = (*i).value / (*j).value;
  113.                                 node.left  = a;
  114.                                 node.right = b;         
  115.                                 node.leftvalue  = (*i).value;
  116.                                 node.rightvalue = (*j).value;
  117.                                 node.opr   = '/';
  118.                         } else if ( ((*i).value != 0) && ((*j).value >= (*i).
  119. value) &&
  120.                                         ((*j).value % (*i).value == 0) )
  121.                         {
  122.                                 node.value = (*j).value / (*i).value;
  123.                                 node.left  = b;
  124.                                 node.right = a;
  125.                                 node.leftvalue  = (*j).value;
  126.                                 node.rightvalue = (*i).value;
  127.                                 node.opr   = '/';
  128.                         }
  129.                                                 
  130.                         if ( (node.value <= MAX_VALUE) && (!state[x].exist[no
  131. de.value]) )
  132. {                              
  133.                                 state[x].nodelist.push_back(node);
  134.                                 state[x].exist[node.value] = true;
  135.                         }                       
  136.                         /
  137.                 }
  138.         }
  139. }
  140. void Solve()
  141. {
  142.         Init();
  143.         
  144.         for (int x = 2; x < STATE_COUNT; x++) {
  145.                 for (int i = 1; i < x; i++) {                  
  146.                         if ( (x & i) == i ) {
  147.                                 int j = x - i;
  148.                                 if (i <= j) {
  149.                                         Merge(i, j, x);         
  150.                                 }
  151.                         }
  152.                 }
  153.         }
  154. }
  155. void PrintExpression(ostream& out, Node node)
  156. {
  157.         if (node.left == -1) {
  158.                 out << node.value;
  159.         } else {
  160.                 NodeList::const_iterator iter;
  161.                
  162.                 out << "(";
  163.                 for (iter = state[node.left].nodelist.begin();
  164.                                 iter != state[node.left].nodelist.end();
  165.                                 iter++)
  166.                 {
  167.                         if ((*iter).value == node.leftvalue) {
  168.                                 PrintExpression(out, *iter);
  169.                                 break;
  170.                         }
  171.                 }
  172.                 out << node.opr;
  173.                 for (iter = state[node.right].nodelist.begin();
  174.                                 iter != state[node.right].nodelist.end();
  175.                                 iter++)
  176.                 {
  177.                         if ((*iter).value == node.rightvalue) {
  178.                                 PrintExpression(out, *iter);
  179.                                 break;
  180.                         }
  181.                 }
  182.                 out << ")";
  183.         }               
  184. }
  185. void Output()
  186. {
  187.         ofstream fout(OUTPUT_FILE);
  188.         int bestValue = -INT_MAX;      
  189.         NodeList::const_iterator iter, bestIter;
  190.         NodeList& nodelist = state[STATE_COUNT-1].nodelist;
  191.         for (iter = nodelist.begin(); iter != nodelist.end(); iter++)
  192.         {      
  193.                 if ( ((*iter).value <= expection) && (bestValue < (*iter).val
  194. ue) ) {
  195.                         bestValue = (*iter).value;
  196.                         bestIter  = iter;
  197.                 }
  198.         }      
  199.         fout << bestValue << endl;
  200.         PrintExpression(fout, *bestIter );
  201.         fout << endl;
  202. }
  203. int main()
  204. {
  205.         ReadData();
  206.         Solve();
  207.         Output();
  208.         return 0;
  209. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值