Advertisement
Guest User

9.1

a guest
Dec 11th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int numberOfPlayers = 473;
  6. const int lastMarble = 70904;
  7.  
  8. //The whole structure of the circle
  9. //For each marble on the circle we keep its prev and its next marble
  10. int nextMarbleOnCircle[lastMarble + 1];
  11. int prevMarbleOnCircle[lastMarble + 1];
  12.  
  13. int nextMarbleToPut;
  14. int currMarble;
  15.  
  16. int currPlayer;
  17. int points[numberOfPlayers]; //points[i] is the number of points of player i
  18.  
  19. void InitGame()
  20. {
  21.     for (int i = 0; i <= lastMarble; i++)
  22.     {
  23.         nextMarbleOnCircle[i] = -1;
  24.         prevMarbleOnCircle[i] = -1;
  25.     }
  26.     nextMarbleOnCircle[0] = 0;
  27.     prevMarbleOnCircle[0] = 0;
  28.  
  29.     currMarble = 0;
  30.     nextMarbleToPut = 1;
  31.  
  32.     currPlayer = 0;
  33.     for (int i = 0; i < numberOfPlayers; i++)
  34.     {
  35.         points[i] = 0;
  36.     }
  37. }
  38.  
  39. //Inserts marble x between marble a and marble b
  40. bool InsertBetweenTwoMarbles(int x, int a, int b)
  41. {
  42.     if (nextMarbleOnCircle[a] != b)
  43.     {
  44.         return false;
  45.     }
  46.  
  47.     nextMarbleOnCircle[a] = x;
  48.     prevMarbleOnCircle[b] = x;
  49.     nextMarbleOnCircle[x] = b;
  50.     prevMarbleOnCircle[x] = a;
  51. }
  52.  
  53. //Removes marble x from the circle
  54. bool RemoveFromCircle(int x)
  55. {
  56.     //Circle would be like ...axb...
  57.     int a = prevMarbleOnCircle[x];
  58.     int b = nextMarbleOnCircle[x];
  59.  
  60.     nextMarbleOnCircle[a] = b;
  61.     prevMarbleOnCircle[b] = a;
  62.     nextMarbleOnCircle[x] = -1;
  63.     prevMarbleOnCircle[x] = -1;
  64. }
  65.  
  66. void DoTurn()
  67. {
  68.     if (nextMarbleToPut % 23 == 0)
  69.     {
  70.         points[currPlayer] += nextMarbleToPut;
  71.         int x = currMarble;
  72.         for (int i = 0; i < 7; i++) x = prevMarbleOnCircle[x];
  73.         currMarble = nextMarbleOnCircle[x];
  74.         points[currPlayer] += x;
  75.         RemoveFromCircle(x);
  76.     }
  77.     else
  78.     {
  79.         int a = nextMarbleOnCircle[currMarble];
  80.         int b = nextMarbleOnCircle[a];
  81.         InsertBetweenTwoMarbles(nextMarbleToPut, a, b);
  82.         currMarble = nextMarbleToPut;
  83.     }
  84.  
  85.     currPlayer = (currPlayer + 1) % numberOfPlayers;
  86.     nextMarbleToPut++;
  87. }
  88.  
  89. int main()
  90. {
  91.     InitGame();
  92.  
  93.     while (nextMarbleToPut <= lastMarble)
  94.     {
  95.         DoTurn();
  96.     }
  97.  
  98.     int maxPoints = 0;
  99.     for (int i = 0; i < numberOfPlayers; i++)
  100.     {
  101.         maxPoints = max(maxPoints, points[i]);
  102.     }
  103.  
  104.     cout << maxPoints << endl;
  105.  
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement