Advertisement
Guest User

Codin Game - Zombies

a guest
Jun 15th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.69 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <map>
  6. #include <unordered_map>
  7. #include <cmath>
  8. #include <float.h>
  9. #include <set>
  10.  
  11.  
  12. using namespace std;
  13.  
  14. const int PLAY_WIDTH = 16000;
  15. const int PLAY_HEIGHT = 9000;
  16.  
  17. const int ZOMBIE_STEP = 400;
  18. const int ASH_STEP = 1000;
  19.  
  20. int calcDistance(map<string, int> &coordinates1, map<string, int> &coordinates2){
  21.     return (sqrt(pow(coordinates1["x"]-coordinates2["x"],2)) + pow(coordinates1["y"] - coordinates2["y"], 2));
  22. }
  23.  
  24. int calcSteps(int distance, int step){
  25.     int counter = 0;
  26.     while (distance > 0) {
  27.         distance -= step;
  28.         counter ++;
  29.     }
  30.     return counter;
  31.    
  32. }
  33.  
  34. bool canAshSaveThem(map<string, int> &human, map<string, int> &ash, map<string, int> &zombie){
  35.     int ashToHuman = calcDistance(ash, human);
  36.     int zombieToHuman = calcDistance(zombie, human);
  37.     int zombieSteps = calcSteps(zombieToHuman, ZOMBIE_STEP);
  38.     int ashSteps = calcSteps(ashToHuman, ASH_STEP);
  39.     cerr << "Ash has "<< ashSteps << " steps to this human \nzombie has " << zombieSteps << " steps to human "<< endl;
  40.     if (ashSteps < zombieSteps) {
  41.         cerr << "Ash can save this human" << endl;
  42.         return true;
  43.     }
  44.     else return false;
  45. }
  46.  
  47. bool humanInDanger(map<string, int> &human, map<string, int> &zombie){
  48.     if (human["x"] == zombie["nextX"] && human["y"] == zombie["nextY"]){
  49.         cerr << "human in danger from zombie" << endl;
  50.         return true;
  51.     }
  52.     else return false;
  53. }
  54. /**
  55.  * Save humans, destroy zombies!
  56.  **/
  57.  
  58.  
  59. int main()
  60. {
  61.  
  62.     // game loop
  63.     while (1) {
  64.         int x;
  65.         int y;
  66.         cin >> x >> y; cin.ignore();
  67.         int humanCount;
  68.         unordered_map <int, map<string, int>> humans;
  69.         vector <int> humanIds;
  70.         cin >> humanCount; cin.ignore();
  71.         for (int i = 0; i < humanCount; i++) {
  72.             int humanId;
  73.             int humanX;
  74.             int humanY;
  75.             cin >> humanId >> humanX >> humanY; cin.ignore();
  76.             map<string, int> coordinates;
  77.             coordinates["x"] = humanX;
  78.             coordinates["y"] = humanY;
  79.             humans[humanId] = coordinates;
  80.             humanIds.push_back(humanId);
  81.         }
  82.         int zombieCount;
  83.         unordered_map <int, map<string, int>> zombies;
  84.         vector <int> zombieIds;
  85.         cin >> zombieCount; cin.ignore();
  86.         for (int i = 0; i < zombieCount; i++) {
  87.             int zombieId;
  88.             int zombieX;
  89.             int zombieY;
  90.             int zombieXNext;
  91.             int zombieYNext;
  92.             cin >> zombieId >> zombieX >> zombieY >> zombieXNext >> zombieYNext; cin.ignore();
  93.             zombieIds.push_back(zombieId);
  94.             map<string, int> coordinates;
  95.             coordinates["x"] = zombieX;
  96.             coordinates["y"] = zombieY;
  97.             coordinates["nextX"] = zombieXNext;
  98.             coordinates["nextY"] = zombieYNext;
  99.             zombies[zombieId] = coordinates;
  100.            
  101.         }
  102.  
  103.         // Write an action using cout. DON'T FORGET THE "<< endl"
  104.         // To debug: cerr << "Debug messages..." << endl;
  105.         int xToGo = 0 , yToGo = 0;
  106.         double distanceToEndageredHuman = DBL_MAX;
  107.         double distanceToZombieCloseBy = DBL_MAX;
  108.  
  109.         map <string,int> ash;
  110.         ash["x"] = x;
  111.         ash["y"] = y;
  112.         set<int> toBeSaved;
  113.         for (auto hit = humanIds.begin(); hit != humanIds.end(); hit++){
  114.             cerr << "human with ID:"<< *hit << " "<< humans[*hit]["x"] << " " << humans[*hit]["y"] << endl;
  115.             for(auto it = zombieIds.begin(); it != zombieIds.end(); it++){
  116.                 cerr << "zombie with ID:"<< *it << " "<< zombies[*it]["x"] << " " << zombies[*it]["y"] << endl;
  117.                 if (canAshSaveThem(humans[*hit], ash, zombies[*it])){
  118.                     toBeSaved.insert(*hit);
  119.                     cerr << "Adeed human with ID: " << *hit << " to the set of humnans that need to be saved.\n";
  120.                 }
  121.                 if(!canAshSaveThem(humans[*hit], ash, zombies[*it])){
  122.                     if(auto iterator = toBeSaved.find(*hit)!=toBeSaved.end()) toBeSaved.erase(iterator);
  123.                     break;
  124.                 }
  125.             }
  126.         }
  127.         for (auto humanID : toBeSaved) cerr << "Human " << humanID << "needs to ne saved" <<endl;
  128.  
  129.         xToGo = humans[*toBeSaved.begin()]["x"];
  130.         yToGo = humans[*toBeSaved.begin()]["y"];
  131.         string output = to_string(xToGo) + " " + to_string(yToGo);
  132.         cerr << "Ash is going to " << to_string(xToGo) + " " + to_string(yToGo)
  133.         << " to save human with the ID of " << *toBeSaved.begin() << endl;
  134.         cout << output << endl; // Your destination coordinates
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement