Advertisement
HasanRasulov

BabyFaces-Heels.cpp

Dec 25th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.07 KB | None | 0 0
  1. #include <iostream>
  2. #include <utility>
  3. #include <list>
  4. #include <algorithm>
  5. #include <iterator>
  6.  
  7. class Competition
  8. {
  9.  
  10.   enum class Teams
  11.   {
  12.     babyface,
  13.     heel,
  14.     nope
  15.   };
  16.  
  17.   class Wrestler
  18.   {
  19.     Teams team;
  20.     size_t _id;
  21.  
  22.   public:
  23.     Wrestler(size_t _id) : _id(_id)
  24.     {
  25.       team = Teams::nope;
  26.     }
  27.  
  28.     void set_team(Teams team)
  29.     {
  30.       this->team = team;
  31.     }
  32.  
  33.     Teams get_team() const
  34.     {
  35.       return this->team;
  36.     }
  37.  
  38.     size_t get_id() const
  39.     {
  40.       return this->_id;
  41.     }
  42.   };
  43.  
  44.   std::list<Wrestler *> *graph;
  45.   const size_t N;
  46.  
  47.   bool isPossible(int, Teams);
  48.  
  49.   bool isCompetitionPossibleUtil(size_t);
  50.  
  51.   bool add_all() { return true; }
  52.  
  53.   void print_teams();
  54.  
  55. public:
  56.   Competition(size_t);
  57.  
  58.   bool add(std::pair<size_t, size_t>);
  59.  
  60.   template <typename T, typename... Types>
  61.   bool add_all(T t, Types... pack);
  62.  
  63.   bool isCompetitionPossible();
  64.  
  65.   Competition *show_competition_schedule();
  66.  
  67.   ~Competition();
  68. };
  69.  
  70. Competition::Competition(size_t N) : N(N)
  71. {
  72.   graph = new std::list<Wrestler *>[N];
  73.  
  74.   for (size_t i = 0; i < N; i++)
  75.   {
  76.     graph[i].push_back(new Wrestler(i));
  77.   }
  78. }
  79.  
  80. Competition::~Competition()
  81. {
  82.  
  83.   for (size_t i = 0; i < N; i++)
  84.   {
  85.     delete graph[i].front();
  86.   }
  87.  
  88.   delete[] graph;
  89. }
  90.  
  91. bool Competition::add(std::pair<size_t, size_t> consent)
  92. {
  93.   if (consent.first >= N || consent.second >= N)
  94.     return false;
  95.  
  96.   graph[consent.first].push_back(graph[consent.second].front());
  97.   graph[consent.second].push_back(graph[consent.first].front());
  98.  
  99.   return true;
  100. }
  101.  
  102. template <typename T, typename... Types>
  103. bool Competition::add_all(T t, Types... pack)
  104. {
  105.  
  106.   if (!add(t))
  107.     return false;
  108.  
  109.   return add_all(pack...);
  110. }
  111.  
  112. void Competition::print_teams()
  113. {
  114.  
  115.   std::cout << "\nBaby-Faces:";
  116.   for (size_t i = 0; i < N; i++)
  117.   {
  118.     const auto it = graph[i].front();
  119.     if (it->get_team() == Teams::babyface)
  120.       std::cout << "[" << it->get_id() << "],";
  121.   }
  122.  
  123.   std::cout << "\nHeels:";
  124.   for (size_t i = 0; i < N; i++)
  125.   {
  126.     const auto it = graph[i].front();
  127.  
  128.     if (it->get_team() == Teams::heel)
  129.       std::cout << "[" << it->get_id() << "],";
  130.   }
  131.   std::cout << std::endl;
  132. }
  133.  
  134. bool Competition::isPossible(int _id, Teams pre)
  135. {
  136.   auto it = graph[_id].begin();
  137.   std::advance(it, 1);
  138.   return !std::any_of(it, graph[_id].end(), [&pre](Wrestler *w) {
  139.     return w->get_team() == pre;
  140.   });
  141. }
  142.  
  143. bool Competition::isCompetitionPossibleUtil(size_t vertex)
  144. {
  145.  
  146.   if (vertex == N)
  147.     return true;
  148.  
  149.   if (isPossible(vertex, Teams::heel))
  150.   {
  151.     graph[vertex].front()->set_team(Teams::heel);
  152.  
  153.     if (isCompetitionPossibleUtil(vertex + 1))
  154.       return true;
  155.  
  156.     graph[vertex].front()->set_team(Teams::nope);
  157.   }
  158.  
  159.   if (isPossible(vertex, Teams::babyface))
  160.   {
  161.     graph[vertex].front()->set_team(Teams::babyface);
  162.  
  163.     if (isCompetitionPossibleUtil(vertex + 1))
  164.       return true;
  165.  
  166.     graph[vertex].front()->set_team(Teams::nope);
  167.   }
  168.  
  169.   return false;
  170. }
  171.  
  172. bool Competition::isCompetitionPossible()
  173. {
  174.  
  175.   if (isCompetitionPossibleUtil(0))
  176.   {
  177.     print_teams();
  178.     return true;
  179.   }
  180.   std::clog << "It is not possible!\n";
  181.   return false;
  182. }
  183.  
  184. Competition *Competition::show_competition_schedule()
  185. {
  186.  
  187.   for (size_t i = 0; i < N; i++)
  188.   {
  189.     for (const auto j : graph[i])
  190.     {
  191.       std::cout << j->get_id() << " ->";
  192.     }
  193.     std::cout << "\n";
  194.   }
  195.  
  196.   return this;
  197. }
  198.  
  199. int main()
  200. {
  201.   const size_t N = 7;
  202.  
  203.   Competition *comp = new Competition(N);
  204.  
  205.   /*
  206.   comp->add(std::make_pair(0, 6));
  207.   comp->add(std::make_pair(6, 2));
  208.   comp->add(std::make_pair(5, 6));
  209.   comp->add(std::make_pair(2, 3));
  210.   comp->add(std::make_pair(1, 3));
  211.   comp->add(std::make_pair(1, 4));
  212. */
  213.  
  214.   comp->add_all(std::make_pair(0, 6),
  215.                 std::make_pair(6, 2),
  216.                 std::make_pair(5, 6),
  217.                 std::make_pair(2, 3),
  218.                 std::make_pair(1, 3),
  219.                 std::make_pair(1, 4));
  220.  
  221.   comp->show_competition_schedule()->isCompetitionPossible();
  222.  
  223.   delete comp;
  224.   return 0;
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement