#include <iostream> #include <string> using namespace std; struct antInformations { char antDirection; int numberOfBounced; }; int getNumberOfAnts(); string getAntsDirection(); antInformations *initAnts(int numberOfAnts, string antsDirections); void printAnts(antInformations *ants, int length); bool isAnyCollision(antInformations *ants, int numberOfAnts); antInformations *collideAnts(antInformations *ants, int numberOfAnts); antInformations *symulateAnts(antInformations *ants, int numberOfAnts); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int numberOfAnts = getNumberOfAnts(); string antsDirections = getAntsDirection(); antInformations *ants = initAnts(numberOfAnts, antsDirections); antInformations *antsToDelete = ants; ants = symulateAnts(ants, numberOfAnts); delete antsToDelete; printAnts(ants, numberOfAnts); return 0; } antInformations *symulateAnts(antInformations *ants, int numberOfAnts) { antInformations *newAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { newAnts[i] = ants[i]; } while (isAnyCollision(newAnts, numberOfAnts)) { antInformations *antsToDelete = newAnts; newAnts = collideAnts(newAnts, numberOfAnts); delete antsToDelete; } return newAnts; } antInformations *collideAnts(antInformations *ants, int numberOfAnts) { antInformations *newAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { newAnts[i] = ants[i]; } for (int i = 0; i < numberOfAnts - 1; i++) { if (ants[i].antDirection == 'P' && ants[i + 1].antDirection == 'L') { newAnts[i].antDirection = 'L'; newAnts[i].numberOfBounced += 1; newAnts[i + 1].antDirection = 'P'; newAnts[i + 1].numberOfBounced += 1; } } return newAnts; } bool isAnyCollision(antInformations *ants, int numberOfAnts) { if (ants[0].antDirection == 'P') { return true; } for (int i = 1; i < numberOfAnts - 1; i++) { if ((ants[i].antDirection == 'L' && ants[i - 1].antDirection == 'P') || (ants[i].antDirection == 'P' && ants[i + 1].antDirection == 'L')) { return true; } } if (ants[numberOfAnts - 1].antDirection == 'L') { return true; } return false; } void printAnts(antInformations *ants, int length) { for (int i = 0; i < length; i++) { cout << ants[i].numberOfBounced << (i == length - 1 ? "" : " "); } } antInformations *initAnts(int numberOfAnts, string antsDirections) { antInformations *tempAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { tempAnts[i].antDirection = antsDirections[i]; tempAnts[i].numberOfBounced = 0; } return tempAnts; } int getNumberOfAnts() { int number = 0; cin >> number; return number; } string getAntsDirection() { string antsDirections = ""; cin >> antsDirections; return antsDirections; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | #include <iostream> #include <string> using namespace std; struct antInformations { char antDirection; int numberOfBounced; }; int getNumberOfAnts(); string getAntsDirection(); antInformations *initAnts(int numberOfAnts, string antsDirections); void printAnts(antInformations *ants, int length); bool isAnyCollision(antInformations *ants, int numberOfAnts); antInformations *collideAnts(antInformations *ants, int numberOfAnts); antInformations *symulateAnts(antInformations *ants, int numberOfAnts); int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int numberOfAnts = getNumberOfAnts(); string antsDirections = getAntsDirection(); antInformations *ants = initAnts(numberOfAnts, antsDirections); antInformations *antsToDelete = ants; ants = symulateAnts(ants, numberOfAnts); delete antsToDelete; printAnts(ants, numberOfAnts); return 0; } antInformations *symulateAnts(antInformations *ants, int numberOfAnts) { antInformations *newAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { newAnts[i] = ants[i]; } while (isAnyCollision(newAnts, numberOfAnts)) { antInformations *antsToDelete = newAnts; newAnts = collideAnts(newAnts, numberOfAnts); delete antsToDelete; } return newAnts; } antInformations *collideAnts(antInformations *ants, int numberOfAnts) { antInformations *newAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { newAnts[i] = ants[i]; } for (int i = 0; i < numberOfAnts - 1; i++) { if (ants[i].antDirection == 'P' && ants[i + 1].antDirection == 'L') { newAnts[i].antDirection = 'L'; newAnts[i].numberOfBounced += 1; newAnts[i + 1].antDirection = 'P'; newAnts[i + 1].numberOfBounced += 1; } } return newAnts; } bool isAnyCollision(antInformations *ants, int numberOfAnts) { if (ants[0].antDirection == 'P') { return true; } for (int i = 1; i < numberOfAnts - 1; i++) { if ((ants[i].antDirection == 'L' && ants[i - 1].antDirection == 'P') || (ants[i].antDirection == 'P' && ants[i + 1].antDirection == 'L')) { return true; } } if (ants[numberOfAnts - 1].antDirection == 'L') { return true; } return false; } void printAnts(antInformations *ants, int length) { for (int i = 0; i < length; i++) { cout << ants[i].numberOfBounced << (i == length - 1 ? "" : " "); } } antInformations *initAnts(int numberOfAnts, string antsDirections) { antInformations *tempAnts = new antInformations[numberOfAnts]; for (int i = 0; i < numberOfAnts; i++) { tempAnts[i].antDirection = antsDirections[i]; tempAnts[i].numberOfBounced = 0; } return tempAnts; } int getNumberOfAnts() { int number = 0; cin >> number; return number; } string getAntsDirection() { string antsDirections = ""; cin >> antsDirections; return antsDirections; } |