Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.57 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. enum {
  9.     ENEMY=-1,
  10.     NEUTRAL=0,
  11.     ME=1
  12. };
  13.  
  14. struct Factory {
  15.     int id;
  16.     int owner;
  17.     int troopCount;
  18.     int production;
  19. }; 
  20.  
  21. struct Relation {
  22.     Factory factory1;
  23.     Factory factory2;
  24.     int distance;
  25. };
  26.  
  27. Relation minDistanceFromFactory(Factory, Relation*, bool);
  28.  
  29. int factoryCount;
  30. int linkCount;
  31. /**
  32.  * Auto-generated code below aims at helping you parse
  33.  * the standard input according to the problem statement.
  34.  **/
  35. int main()
  36. {
  37.      // the number of factories
  38.     cin >> factoryCount; cin.ignore();
  39.     // the number of links between factories
  40.     cin >> linkCount; cin.ignore();
  41.    
  42.     Factory factories[factoryCount];
  43.     Relation relations[linkCount];
  44.    
  45.     for (int i=0; i < factoryCount; i++) {
  46.         Factory factory;
  47.         factory.id = i;
  48.         factories[i] = factory;
  49.     }
  50.  
  51.     for (int i = 0; i < linkCount; i++) {
  52.         int factory1;
  53.         int factory2;
  54.         int distance;
  55.         cin >> factory1 >> factory2 >> distance; cin.ignore();
  56.         relations[i].factory1 = factories[factory1];
  57.         relations[i].factory2 = factories[factory2];
  58.         relations[i].distance = distance;
  59.     }
  60.  
  61.     // game loop
  62.     while (1) {
  63.         int entityCount; // the number of entities (e.g. factories and troops)
  64.         cin >> entityCount; cin.ignore();
  65.         for (int i = 0; i < entityCount; i++) {
  66.             int entityId;
  67.             string entityType;
  68.             int owner;
  69.             int arg2;
  70.             int arg3;
  71.             int arg4;
  72.             int arg5;
  73.             cin >> entityId >> entityType >> owner >> arg2 >> arg3 >> arg4 >> arg5; cin.ignore();
  74.             if (entityType == "FACTORY") {
  75.                 factories[entityId].owner = owner;
  76.                 factories[entityId].troopCount = arg2;
  77.                 factories[entityId].production = arg3;
  78.             }
  79.         }
  80.         int source = -1;
  81.         int destination;
  82.         int cyborgs;
  83.         int distance = 999;
  84.         for(int i = 0; i < factoryCount; i++) {
  85.             if(factories[i].owner == ME) {
  86.                 if (factories[i].troopCount > 0) {
  87.                     Relation relation = minDistanceFromFactory(factories[i], relations, true);
  88.                     if (relation.distance <= distance) {
  89.                         distance = relation.distance;
  90.                     } else {
  91.                         continue;
  92.                     }
  93.                     source = i;
  94.                     if (relation.factory1.id == factories[i].id) {
  95.                         destination = relation.factory2.id;
  96.                     } else {
  97.                         destination = relation.factory1.id;
  98.                     }
  99.                     cyborgs = (factories[i].troopCount > 20) ? 20 : factories[i].troopCount;
  100.                 }
  101.             }
  102.         }
  103.         if (source != -1) {
  104.             cout << "MOVE " << source << " " << destination << " " << cyborgs << endl;
  105.         } else {
  106.             cout << "WAIT" << endl;
  107.         }
  108.        
  109.        
  110.  
  111.         // Write an action using cout. DON'T FORGET THE "<< endl"
  112.         // To debug: cerr << "Debug messages..." << endl;
  113.  
  114.  
  115.         // Any valid action, such as "WAIT" or "MOVE source destination cyborgs"
  116.     }
  117. }
  118.  
  119. Relation minDistanceFromFactory(Factory factory, Relation* relations, bool exceptMyFactories)
  120. {
  121.     int id_relation, min_distance = 999;
  122.     for(int i=0; i < linkCount; i++) {
  123.         if (relations[i].factory1.id == factory.id) {
  124.             if (exceptMyFactories && relations[i].factory2.owner == ME) {
  125.                 continue;
  126.             }
  127.         }
  128.         if (relations[i].factory2.id == factory.id) {
  129.             if (exceptMyFactories && relations[i].factory1.owner == ME) {
  130.                 continue;
  131.             }
  132.         }
  133.  
  134.         if (relations[i].distance <= min_distance) {
  135.             min_distance = relations[i].distance;
  136.             cerr << min_distance << endl;
  137.             id_relation = i;
  138.         }
  139.     }
  140.     return relations[id_relation];
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement