Advertisement
MiinaMagdy

162 - Beggar My Neighbour

Sep 5th, 2022
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define endl '\n'
  7. #define sz(x) int(x.size())
  8. #define all(x) x.begin(), x.end()
  9.  
  10. bool is_face(string s) {
  11.     return isalpha(s[1]) && s[1] != 'T';
  12. }
  13.  
  14. void print(deque<string> p) {
  15.     while (sz(p)) cout << p.back() << " ", p.pop_back();
  16.     cout << endl;
  17. }
  18.  
  19. int main() {
  20.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  21.     string s;
  22.     while (1) {
  23.         cin >> s;
  24.         if (s == "#") break;
  25.         deque<string> heap;
  26.         vector<deque<string>> p(2);
  27.         p[0].push_back(s);
  28.         for (int i = 1; i < 52; i++) {
  29.             cin >> s;
  30.             p[i % 2].push_back(s);
  31.         }
  32.         int turn = 1;
  33.         bool game_over = false;
  34.         //cout << "0: ";
  35.         //print(p[0]);
  36.         heap.push_back(p[0].back()), p[0].pop_back();
  37.         int winner = 0;
  38.         map<char, int> mp = { {'A', 4}, {'K', 3}, {'Q', 2}, {'J', 1} };
  39.         while (!game_over) {
  40.             //cout << turn << ": ";
  41.             //print(p[turn]);
  42.             if (is_face(heap.back())) {
  43.                 int plays = mp[heap.back()[1]];
  44.                 while (plays-- && !p[turn].empty() && !is_face(p[turn].back())) {
  45.                     heap.push_back(p[turn].back());
  46.                     p[turn].pop_back();
  47.                 }
  48.                 if (plays == -1) {
  49.                     while (sz(heap)) p[!turn].push_front(heap.front()), heap.pop_front();
  50.                 }
  51.                 else if (p[turn].empty()) {
  52.                     game_over = true;
  53.                     winner = !turn;
  54.                 }
  55.                 else heap.push_back(p[turn].back()), p[turn].pop_back();
  56.             }
  57.             else {
  58.                 if (p[turn].empty()) game_over = true, winner = !turn;
  59.                 else heap.push_back(p[turn].back()), p[turn].pop_back();
  60.             }
  61.             turn = !turn;
  62.         }
  63.         cout << !winner + 1 << " " << setw(2) << sz(p[winner]) << endl;
  64.     }
  65.    
  66. }
  67.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement