Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <map>
- #include <unordered_map>
- #include <cmath>
- #include <float.h>
- #include <set>
- using namespace std;
- const int PLAY_WIDTH = 16000;
- const int PLAY_HEIGHT = 9000;
- const int ZOMBIE_STEP = 400;
- const int ASH_STEP = 1000;
- int calcDistance(map<string, int> &coordinates1, map<string, int> &coordinates2){
- return (sqrt(pow(coordinates1["x"]-coordinates2["x"],2)) + pow(coordinates1["y"] - coordinates2["y"], 2));
- }
- int calcSteps(int distance, int step){
- int counter = 0;
- while (distance > 0) {
- distance -= step;
- counter ++;
- }
- return counter;
- }
- bool canAshSaveThem(map<string, int> &human, map<string, int> &ash, map<string, int> &zombie){
- int ashToHuman = calcDistance(ash, human);
- int zombieToHuman = calcDistance(zombie, human);
- int zombieSteps = calcSteps(zombieToHuman, ZOMBIE_STEP);
- int ashSteps = calcSteps(ashToHuman, ASH_STEP);
- cerr << "Ash has "<< ashSteps << " steps to this human \nzombie has " << zombieSteps << " steps to human "<< endl;
- if (ashSteps < zombieSteps) {
- cerr << "Ash can save this human" << endl;
- return true;
- }
- else return false;
- }
- bool humanInDanger(map<string, int> &human, map<string, int> &zombie){
- if (human["x"] == zombie["nextX"] && human["y"] == zombie["nextY"]){
- cerr << "human in danger from zombie" << endl;
- return true;
- }
- else return false;
- }
- /**
- * Save humans, destroy zombies!
- **/
- int main()
- {
- // game loop
- while (1) {
- int x;
- int y;
- cin >> x >> y; cin.ignore();
- int humanCount;
- unordered_map <int, map<string, int>> humans;
- vector <int> humanIds;
- cin >> humanCount; cin.ignore();
- for (int i = 0; i < humanCount; i++) {
- int humanId;
- int humanX;
- int humanY;
- cin >> humanId >> humanX >> humanY; cin.ignore();
- map<string, int> coordinates;
- coordinates["x"] = humanX;
- coordinates["y"] = humanY;
- humans[humanId] = coordinates;
- humanIds.push_back(humanId);
- }
- int zombieCount;
- unordered_map <int, map<string, int>> zombies;
- vector <int> zombieIds;
- cin >> zombieCount; cin.ignore();
- for (int i = 0; i < zombieCount; i++) {
- int zombieId;
- int zombieX;
- int zombieY;
- int zombieXNext;
- int zombieYNext;
- cin >> zombieId >> zombieX >> zombieY >> zombieXNext >> zombieYNext; cin.ignore();
- zombieIds.push_back(zombieId);
- map<string, int> coordinates;
- coordinates["x"] = zombieX;
- coordinates["y"] = zombieY;
- coordinates["nextX"] = zombieXNext;
- coordinates["nextY"] = zombieYNext;
- zombies[zombieId] = coordinates;
- }
- // Write an action using cout. DON'T FORGET THE "<< endl"
- // To debug: cerr << "Debug messages..." << endl;
- int xToGo = 0 , yToGo = 0;
- double distanceToEndageredHuman = DBL_MAX;
- double distanceToZombieCloseBy = DBL_MAX;
- map <string,int> ash;
- ash["x"] = x;
- ash["y"] = y;
- set<int> toBeSaved;
- for (auto hit = humanIds.begin(); hit != humanIds.end(); hit++){
- cerr << "human with ID:"<< *hit << " "<< humans[*hit]["x"] << " " << humans[*hit]["y"] << endl;
- for(auto it = zombieIds.begin(); it != zombieIds.end(); it++){
- cerr << "zombie with ID:"<< *it << " "<< zombies[*it]["x"] << " " << zombies[*it]["y"] << endl;
- if (canAshSaveThem(humans[*hit], ash, zombies[*it])){
- toBeSaved.insert(*hit);
- cerr << "Adeed human with ID: " << *hit << " to the set of humnans that need to be saved.\n";
- }
- if(!canAshSaveThem(humans[*hit], ash, zombies[*it])){
- if(auto iterator = toBeSaved.find(*hit)!=toBeSaved.end()) toBeSaved.erase(iterator);
- break;
- }
- }
- }
- for (auto humanID : toBeSaved) cerr << "Human " << humanID << "needs to ne saved" <<endl;
- xToGo = humans[*toBeSaved.begin()]["x"];
- yToGo = humans[*toBeSaved.begin()]["y"];
- string output = to_string(xToGo) + " " + to_string(yToGo);
- cerr << "Ash is going to " << to_string(xToGo) + " " + to_string(yToGo)
- << " to save human with the ID of " << *toBeSaved.begin() << endl;
- cout << output << endl; // Your destination coordinates
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement