Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <chrono>
  6. #include <cmath>
  7. #include <float.h>
  8. #include <climits>
  9. using namespace std::chrono;
  10.  
  11. high_resolution_clock::time_point now = high_resolution_clock::now();
  12. #define TIME duration_cast<duration<double>>(high_resolution_clock::now() - now).count()
  13.  
  14. using namespace std;
  15.  
  16. const int height = 10;
  17. const int width = 19;
  18.  
  19. const int grid_size = height * width;
  20.  
  21. const int robotCount = 10;
  22.  
  23. int sim_count = 0;
  24. struct vec2
  25. {
  26.     int x;
  27.     int y;
  28. };
  29. struct Robot;
  30. struct Grid;
  31.  
  32. template<class T, class U>
  33. vec2 sum(T a, U b)
  34. {
  35.     return { a.x + b.x, a.y + b.y };
  36. }
  37. template <class T>
  38.  
  39. int findIndex(T a)
  40. {
  41.     return a.y * width + a.x;
  42. }
  43.  
  44. int findIndex(int x, int y)
  45. {
  46.     return y * width + x;
  47. }
  48.  
  49. enum Type
  50. {
  51.     Up = 1 << 0,
  52.     Right = 1 << 1,
  53.     Down = 1 << 2,
  54.     Left = 1 << 3,
  55.     Void,
  56.     Empty
  57. };
  58. struct Grid : vec2
  59. {
  60.     Grid()
  61.     {
  62.         for (int &s : states_used)
  63.             s = 0;
  64.     }
  65.  
  66.     Grid(int x, int y, Type type)
  67.     {
  68.         this->x = x;
  69.         this->y = y;
  70.         this->type = type;
  71.  
  72.         for (int &s : states_used)
  73.             s = 0;
  74.     }
  75.  
  76.     void init(int x, int y, Type type)
  77.     {
  78.         this->x = x;
  79.         this->y = y;
  80.         this->type = type;
  81.     }
  82.     Type type;
  83.     int states_used[robotCount];
  84. };
  85. struct Robot : vec2
  86. {
  87.     Robot() {
  88.         alive = false;
  89.     }
  90.     Robot(int x, int y, Type type)
  91.     {
  92.         this->x = x;
  93.         this->y = y;
  94.         this->type = type;
  95.         alive = true;
  96.     }
  97.     void init(int x, int y, Type type, int id)
  98.     {
  99.         this->x = x;
  100.         this->y = y;
  101.         this->type = type;
  102.         this->id = id;
  103.         alive = true;
  104.     }
  105.     Type type;
  106.     int id;
  107.     bool alive;
  108. };
  109.  
  110. struct State
  111. {
  112.     Grid grid[grid_size];
  113.     Robot robots[robotCount];
  114.  
  115.  
  116.     void generate_arrows()
  117.     {
  118.         for (auto & cell : grid)
  119.         {
  120.             if (cell.type != Empty) continue;
  121.             if (rand() % 100 > 10) continue;
  122.             int type_index = rand() % 4;
  123.  
  124.             Type type;
  125.             switch (type_index)
  126.             {
  127.             case 0:
  128.                 type = Up;
  129.                 break;
  130.             case 1:
  131.                 type = Down;
  132.                 break;
  133.             case 2:
  134.                 type = Left;
  135.                 break;
  136.             case 3:
  137.                 type = Right;
  138.                 break;
  139.  
  140.             }
  141.  
  142.             cell.type = type;
  143.  
  144.         }
  145.  
  146.  
  147.  
  148.     }
  149.  
  150.  
  151.     void simulate_setup()
  152.     {
  153.         for (auto & r : robots)
  154.         {
  155.             if (!r.alive) continue;
  156.             Grid &cell = grid[findIndex(r)];
  157.             if (cell.type != Empty)
  158.             {
  159.                 r.type = cell.type;
  160.             }
  161.             cell.states_used[r.id] |= r.type;
  162.         }
  163.     }
  164.     int simulate_game()
  165.     {
  166.         simulate_setup();
  167.  
  168.         int score = 0;
  169.         int alive = alive_robots();
  170.         while (alive)
  171.         {
  172.             score += alive;
  173.             for (auto &robot : robots)
  174.             {
  175.                 if (!robot.alive) continue;
  176.  
  177.                 switch (robot.type)
  178.                 {
  179.                 case Up:
  180.                     robot.y--;
  181.                     if (robot.y < 0)
  182.                     {
  183.                         robot.y = height - 1;
  184.                     }
  185.                     break;
  186.                 case Down:
  187.                     robot.y++;
  188.                     if (robot.y > height - 1)
  189.                     {
  190.                         robot.y = 0;
  191.                     }
  192.                     break;
  193.                 case Left:
  194.                     robot.x--;
  195.                     if (robot.x < 0)
  196.                     {
  197.                         robot.x = width - 1;
  198.                     }
  199.                     break;
  200.                 case Right:
  201.                     robot.x++;
  202.                     if (robot.x > width - 1)
  203.                     {
  204.                         robot.x = 0;
  205.                     }
  206.                     break;
  207.                 }
  208.  
  209.                 Grid &cell = grid[findIndex(robot)];
  210.                 if (cell.type == Void || cell.states_used[robot.id] & robot.type)
  211.                 {
  212.                     robot.alive = false;
  213.                     alive--;
  214.                     continue;
  215.                 }
  216.                 //TODO IDETI JEI ROBOTAS I ZENGIA I INFINITE LOOP
  217.                 cell.states_used[robot.id] |= robot.type;
  218.  
  219.                 // Robotas vis dar gyvas, izenge i nauja cell
  220.                 if (cell.type != Type::Empty)
  221.                 {
  222.                     robot.type = cell.type;
  223.                 }
  224.  
  225.             }
  226.         }
  227.         return score;
  228.     }
  229.     bool out_of_bounds(vec2 pos)
  230.     {
  231.         return pos.x < 0 || pos.y < 0 || pos.x > width - 1 || pos.y > height - 1;
  232.     }
  233.     int alive_robots()
  234.     {
  235.         int alive = 0;
  236.         for (auto r : robots)
  237.         {
  238.             if (r.alive)
  239.                 alive++;
  240.         }
  241.         return alive;
  242.     }
  243. };
  244.  
  245.  
  246. struct Agent
  247. {
  248.     State state;
  249.     int bestScore;
  250.     State bestState;
  251.     void read()
  252.     {
  253.         for (int y = 0; y < 10; y++) {
  254.             string line;
  255.             cin >> line; cin.ignore();
  256.             for (int x = 0; x < line.size(); x++)
  257.             {
  258.                 Grid &cell = state.grid[findIndex(x, y)];
  259.                 Type type;
  260.                 if (line[x] == 'U') type = Up;
  261.                 else if (line[x] == 'D') type = Down;
  262.                 else if (line[x] == 'L') type = Left;
  263.                 else if (line[x] == 'R') type = Right;
  264.                 else if (line[x] == '#') type = Void;
  265.                 else type = Empty;
  266.                 cell.init(x, y, type);
  267.             }
  268.         }
  269.         now = high_resolution_clock::now();
  270.  
  271.         int robotCount;
  272.         cin >> robotCount; cin.ignore();
  273.         for (int i = 0; i < robotCount; i++) {
  274.             int x;
  275.             int y;
  276.             char direction;
  277.             cin >> x >> y >> direction; cin.ignore();
  278.             Type type;
  279.             if (direction == 'U') type = Up;
  280.             else if (direction == 'D') type = Down;
  281.             else if (direction == 'L') type = Left;
  282.             else if (direction == 'R') type = Right;
  283.             state.robots[i].init(x, y, type, i);
  284.         }
  285.     }
  286.     void think()
  287.     {
  288.         bestScore = -INT_MAX;
  289.         while (TIME * 1000 < 998)
  290.         {
  291.             sim_count++;
  292.  
  293.             State sim_state = state;
  294.             sim_state.generate_arrows();
  295.             int score = sim_state.simulate_game();
  296.             if (score > bestScore)
  297.             {
  298.                 bestScore = score;
  299.                 bestState = sim_state;
  300.             }
  301.         }
  302.  
  303.     }
  304.     void output()
  305.     {
  306.         cerr << "SIMULIACIJU PRAEJO " << sim_count << " o score : " << bestScore << endl;
  307.         struct Out { int x; int y; char dir; };
  308.         vector<Out> out;
  309.  
  310.         for (auto &cell : bestState.grid)
  311.         {
  312.             if (cell.type == Empty || cell.type == Void) continue;
  313.             char dir;
  314.             switch (cell.type)
  315.             {
  316.             case Up:
  317.                 dir = 'U';
  318.                 break;
  319.             case Down:
  320.                 dir = 'D';
  321.                 break;
  322.             case Left:
  323.                 dir = 'L';
  324.                 break;
  325.             case Right:
  326.                 dir = 'R';
  327.                 break;
  328.  
  329.             }
  330.  
  331.             out.push_back({ cell.x , cell.y, dir });
  332.         }
  333.  
  334.         for (int i = 0; i < out.size(); i++)
  335.         {
  336.             cout << out[i].x << " " << out[i].y << " " << out[i].dir;
  337.             if (i < out.size() - 1) cout << " ";
  338.         }cout << endl;
  339.     }
  340.  
  341. };
  342. int main()
  343. {
  344.     srand(time(0));
  345.  
  346.     Agent agent;
  347.  
  348.     agent.read();
  349.     agent.think();
  350.     agent.output();
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement