Advertisement
Guest User

Play - 1,2,3 cards Alternate

a guest
Apr 1st, 2020
1,358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. // Given a deck: you can pick 1,2 or 3 first cards and alternate with your opponent
  2. // How do you maximize your points.
  3. // [1,1,1,1,1]   // You choose 3 first cards.
  4. // [100,1,1,1,1] // You choose only one card. So your opponent can't choose 100.
  5. #include <iostream>     // std::cout
  6. #include <vector>
  7. #include <map>
  8.  
  9. using namespace std;
  10.  
  11. typedef pair<int,int> pairResult;
  12.  
  13. pairResult fRecMemo(const vector<int> &v, int idx, map<int, pairResult>& results) {
  14.     if(idx<0)
  15.         return pairResult(0,0);
  16.     if(results.count(idx))
  17.         return results[idx];
  18.     pairResult bestResult;
  19.     int curHand = 0;
  20.     for(int numCards = 1; numCards <=3; numCards++) {
  21.         int curIdx = idx - (numCards-1);
  22.         if(curIdx < 0)
  23.             break;
  24.         curHand += v[curIdx];
  25.         pairResult oponentResult = fRecMemo(v, curIdx-1, results);
  26.         pairResult curResult = pairResult(curHand + oponentResult.second, oponentResult.first); // swapping
  27.         bestResult = numCards == 1 ? curResult : max(bestResult, curResult);
  28.     }
  29.     results[idx] = bestResult;
  30.     return results[idx];
  31. }
  32. pairResult fRecMemo(const vector<int> &v) {
  33.     map<int, pairResult> results;
  34.     return fRecMemo(v, v.size()-1, results);
  35. }
  36.  
  37. pairResult fBottomUPDP(const vector<int> &v) {
  38.     if (v.size()==0)
  39.         return make_pair(0,0);
  40.  
  41.     vector<pair<int,int> > results;
  42.     for(int i=0; i<v.size(); i++) {
  43.         int curHand = 0;
  44.         pair<int,int> bestResult;
  45.         for(int numCards = 1; numCards<=min(3,i+1); numCards++) {
  46.             curHand += v[i+1-numCards];
  47.             pair<int,int> oponenteResult(0,0);
  48.             if(i-numCards>=0)
  49.                 oponenteResult = results[i-numCards];
  50.             pair<int,int> curResult(curHand + oponenteResult.second, oponenteResult.first);
  51.             if(numCards==1 || curResult.first>bestResult.first)
  52.                 bestResult = curResult;
  53.         }
  54.         results.push_back(bestResult);
  55.     }
  56.     return results.back();
  57. }
  58.  
  59. pairResult fBottomUPDPTempVars(const vector<int> &v) {
  60.     pair<int,int> prevRes[3];
  61.     prevRes[0] = pair<int,int>(0,0);
  62.     for(int i=0; i<v.size(); i++) {
  63.         int curHand = 0;
  64.         pair<int,int> maxR;
  65.         for(int nC=1; nC<=3 && nC<=i+1; nC++) {
  66.             curHand += v[i+1-nC];
  67.             pair<int,int> prevR = prevRes[nC-1];
  68.             pair<int,int> curR(curHand+prevR.second,prevR.first);
  69.             if(nC==1 || curR.first>maxR.first)
  70.                 maxR = curR;
  71.         }
  72.         prevRes[2] = prevRes[1];
  73.         prevRes[1] = prevRes[0];
  74.         prevRes[0] = maxR;
  75.     }
  76.     return prevRes[0];
  77. }
  78.  
  79.  
  80. int main() {
  81.     vector<int> d; // deck
  82.     d.push_back(-20);
  83.     d.push_back(10);
  84.     d.push_back(1);
  85.     d.push_back(3);
  86.     d.push_back(2);
  87.     d.push_back(1);
  88.  
  89.     pairResult p4 = fRecMemo(d);
  90.     cout << "Resultado: " << p4.first << " " << p4.second << endl;
  91.  
  92.     cout << endl << "Compare Memoized x Non-Memoized" << endl;
  93.  
  94.     vector<int> d2;
  95.     for(int i=0; i<3; i++) {
  96.         for(int j=1; j<=2; j++)
  97.             d2.push_back(j);
  98.         // slow after 34 elements more than 1B calls
  99.         cout << d2.size() << endl;
  100.         pairResult p3 = fBottomUPDP(d2);
  101.         cout << p3.first << " " << p3.second << "\t";
  102.         cout << "Bottom UP DP " << endl;
  103.         p3 = fBottomUPDPTempVars(d2);
  104.         cout << p3.first << " " << p3.second << "\t";
  105.         cout << "Bottom UP DP Temp Vars " << endl;
  106.         p3 = fRecMemo(d2);
  107.         cout << p3.first << " " << p3.second << "\t";
  108.         cout << "Memoized " << endl;
  109.     }
  110.  
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement