Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- using namespace std;
- int countCollisions(const vector<pair<int, int>>& centers) {
- int n = centers.size();
- int collisionCount = 0; // Variable to store the number of colliding pairs
- // Loop through all pairs of objects
- for (int i = 0; i < n; ++i) {
- for (int j = i + 1; j < n; ++j) {
- int x1 = centers[i].first;
- int y1 = centers[i].second;
- int x2 = centers[j].first;
- int y2 = centers[j].second;
- // Check collision conditions: |x1 - x2| <= 2 and |y1 - y2| <= 2
- if (abs(x1 - x2) <= 2 && abs(y1 - y2) <= 2) {
- ++collisionCount;
- }
- }
- }
- return collisionCount; // Return the total number of colliding pairs
- }
- int main() {
- // Example input: List of object centers
- vector<pair<int, int>> centers = {
- {1, 1}, {2, 2}, {0, 4},{1,1}
- };
- // Calculate and output the number of colliding pairs
- int result = countCollisions(centers);
- cout << "Number of colliding pairs: " << result << endl; // Expected output: 3
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement