Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <algorithm>
  5.  
  6.  
  7. enum TYPE{
  8.     SQUARE,
  9.     CIRCLE,
  10.     TRIANGLE,
  11. };
  12.  
  13. struct _card
  14. {
  15.     TYPE type;
  16.     int amount;
  17.     _card( int a, TYPE t) { type = t; amount = a;};
  18.     bool operator <(const _card & lhs) const {
  19.         return (type < lhs.type || amount < lhs.amount) ;
  20.     }
  21.     bool operator <=(const _card & lhs) const {
  22.         return (type <= lhs.type || amount <= lhs.amount) ;
  23.     }
  24.     bool operator ==(const _card & lhs) const {
  25.         return (type == lhs.type || amount == lhs.amount) ;
  26.     }
  27.  
  28. };
  29.  
  30.  
  31.  
  32. inline bool canMatch(_card a, _card b)
  33. {
  34.     return((a.type == b.type && a.amount != b.amount) || (a.type == b.type && a.amount == b.amount) );
  35. }
  36.  
  37. inline bool canMatch3(_card a, _card b, _card c)
  38. {
  39.     return((a.type == b.type && a.type == c.type)
  40.            && ((a.amount != b.amount && b.amount != c.amount && a.amount != c.amount) ||
  41.                ( a.amount == b.amount && a.amount == c.amount )));
  42. }
  43.  
  44.  
  45. inline bool cannotMatch(_card a, _card b)
  46. {
  47.     return(a.type != b.type && a.amount != b.amount);
  48. }
  49.  
  50. inline bool cannotMatch3(_card a, _card b, _card c)
  51. {
  52.     return((a.type != b.type && b.type != c.type && a.type != c.type) &&
  53.            (a.amount != b.amount && b.amount != c.amount && a.amount != c.amount));
  54. }
  55.  
  56. inline _card GetgameCard(std::string card) {
  57.     if(card.compare("um quadrado") == 0) return _card(1, SQUARE);
  58.     else if(card.compare("dois quadrados") == 0) return _card(2, SQUARE);
  59.     else if(card.compare("tres quadrados") == 0) return _card(3, SQUARE);
  60.     else if(card.compare("um triangulo") == 0) return _card(1, TRIANGLE);
  61.     else if(card.compare("dois triangulos") == 0) return _card(2, TRIANGLE);
  62.     else if(card.compare("tres triangulos") == 0) return _card(3, TRIANGLE);
  63.     else if(card.compare("um circulo") == 0) return _card(1, CIRCLE);
  64.     else if(card.compare("dois circulos") == 0) return _card(2, CIRCLE);
  65.     else if(card.compare("tres circulos") == 0) return _card(3, CIRCLE);
  66.  
  67.     return _card(0,(TYPE)0);
  68. }
  69.  
  70. class SetGameManager {
  71. public:
  72.     SetGameManager() {};
  73.  
  74. public:
  75.     std::vector<_card> gameCards;
  76.  
  77. public:
  78.  
  79.     void AddCard(_card gameCard);
  80.     void RemoveCards() { gameCards.clear(); };
  81.     unsigned int CountSet();
  82. };
  83.  
  84. inline void SetGameManager::AddCard(_card gameCard)
  85. {
  86.     gameCards.push_back(gameCard);
  87. }
  88.  
  89. inline unsigned int SetGameManager::CountSet()
  90. {
  91.     unsigned int SetMatches = 0;
  92.  
  93.     unsigned int i1 = 0;
  94.  
  95.     std::sort(this->gameCards.begin(), this->gameCards.end());
  96.  
  97.     while(gameCards.size() > 2)
  98.     {
  99.         auto continue_outer_loop(true);
  100.         for(unsigned int i2=1; continue_outer_loop && (i2<gameCards.size()); i2++)
  101.         {
  102.             if(i2 == 2) continue;
  103.             auto const p_match_3_func
  104.             (
  105.                 cannotMatch(gameCards.at(i1), gameCards.at(i2))
  106.                 ?
  107.                 &cannotMatch3
  108.                 :
  109.                 &canMatch3
  110.             );
  111.             for(unsigned int i3=2; i3<gameCards.size(); i3++)
  112.             {
  113.                 if((*p_match_3_func)(gameCards.at(i1), gameCards.at(i2), gameCards.at(i3)))
  114.                 {
  115.                     SetMatches++;
  116.                     gameCards.erase(gameCards.begin()+i3);
  117.                             gameCards.erase(gameCards.begin()+i2);
  118.                     continue_outer_loop = false;
  119.                     break;
  120.                 }
  121.             }
  122.         }
  123.         gameCards.erase(gameCards.begin());
  124.     }
  125.  
  126.  
  127.     return SetMatches;
  128. }
  129.  
  130. int main()
  131. {
  132.     SetGameManager Manager;
  133.  
  134.     int cardAmount;
  135.    // std::cout << "Enter the amount of cards:" << std::endl;
  136.     std::cin >> cardAmount;
  137.  
  138.  
  139.  
  140.     std::string card;
  141.     while(cardAmount != 0)
  142.     {
  143.         std::cin.ignore();
  144.         for(int i=0; i<cardAmount; i++)
  145.         {
  146.             std::getline(std::cin, card);
  147.            // std::cout << GetgameCard(card) << std::endl;
  148.             Manager.AddCard(GetgameCard(card));
  149.         }
  150.         //std::cout << "cannotMatch3: " << cannotMatch3(Manager.gameCards.at(0),Manager.gameCards.at(1), Manager.gameCards.at(2)) << std::endl;
  151.         std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
  152.  
  153.         std::cout << Manager.CountSet() << std::endl;
  154.  
  155.         std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now();
  156.         auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
  157.         std::cout << "Miliseconds: " << duration << std::endl;
  158.  
  159.  
  160.         Manager.RemoveCards();
  161.         std::cin >> cardAmount;
  162.     }
  163.  
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement