Advertisement
mateuspl

URI: 1522 - Jogo das Pilhas

Feb 10th, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string.h>
  4.  
  5. using namespace std;
  6.  
  7. vector<short>   *stackA = NULL,
  8.                 *stackB = NULL,
  9.                 *stackC = NULL;
  10. bool possibles[1000000];
  11.  
  12. int toKey(short a, short b, short c)
  13. {
  14.     int key = 0;
  15.  
  16.     if (a > 0) key += a * 10000;
  17.     if (b > 0) key += b * 100;
  18.     if (c > 0) key += c;
  19.  
  20.     return key;
  21. }
  22.  
  23. bool isPossible(short a, short b, short c)
  24. {
  25.     if (a == -1 && b == -1 && c == -1)
  26.         return true;
  27.  
  28.     short cardA = a >= 0 ? stackA->at(a) : -1;
  29.     short cardB = b >= 0 ? stackB->at(b) : -1;
  30.     short cardC = c >= 0 ? stackC->at(c) : -1;
  31.  
  32.     if (a >= 0 && b >= 0 && (cardA + cardB) % 3 == 0)
  33.         if (possibles[toKey(a - 1, b - 1, c)])
  34.             if (isPossible(a - 1, b - 1, c))
  35.                 return true;
  36.  
  37.     if (a >= 0 && c >= 0 && (cardA + cardC) % 3 == 0)
  38.         if (possibles[toKey(a - 1, b, c - 1)])
  39.             if (isPossible(a - 1, b, c - 1))
  40.                 return true;
  41.  
  42.    
  43.     if (b >= 0 && c >= 0 && (cardB + cardC) % 3 == 0)
  44.         if (possibles[toKey(a, b - 1, c - 1)])
  45.             if (isPossible(a, b - 1, c - 1))
  46.                 return true;
  47.  
  48.    
  49.     if (a >= 0 && b >= 0 && c >= 0 && (cardA + cardB + cardC) % 3 == 0)
  50.         if (possibles[toKey(a - 1, b - 1, c - 1)])
  51.             if (isPossible(a - 1, b - 1, c - 1))
  52.                 return true;
  53.  
  54.     possibles[toKey(a, b, c)] = false;
  55.     return false;
  56. }
  57.  
  58. int main()
  59. {
  60.     short n;
  61.  
  62.     while (true)
  63.     {
  64.         cin >> n;
  65.         if ( ! n) break;
  66.  
  67.         stackA = new vector<short>;
  68.         stackB = new vector<short>;
  69.         stackC = new vector<short>;
  70.  
  71.         while (n--)
  72.         {
  73.             short cardA, cardB, cardC;
  74.             cin >> cardA >> cardB >> cardC;
  75.  
  76.             if (cardA % 3) stackA->push_back(cardA);
  77.             if (cardB % 3) stackB->push_back(cardB);
  78.             if (cardC % 3) stackC->push_back(cardC);
  79.         }
  80.  
  81.         short   a = stackA->size() - 1,
  82.                 b = stackB->size() - 1,
  83.                 c = stackC->size() - 1;
  84.  
  85.         memset(possibles, true, 1000000);
  86.  
  87.         cout << (isPossible(a, b, c) ? 1 : 0) << endl;
  88.  
  89.         delete stackA;
  90.         delete stackB;
  91.         delete stackC;
  92.     }
  93.  
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement