Last active
December 6, 2018 21:08
-
-
Save Caellian/2eae4d7a9f163ca4a64dbd7d427901dc to your computer and use it in GitHub Desktop.
MIT - Find which point is furthest from others.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <map> | |
| #include <vector> | |
| #include <utility> | |
| #include <cmath> | |
| #include <cstdlib> | |
| typedef struct { | |
| int posX, posY; | |
| int name; | |
| int size = 0; | |
| bool infinite = false; | |
| } Point; | |
| int main(int argc, char const *argv[]) { | |
| std::vector<Point> points; | |
| int sx = 0, sy = 0, pc = 1; | |
| while (true) { | |
| Point created; | |
| created.name = pc++; | |
| std::cout << "Enter X & Y of point " << created.name << ": "; | |
| std::cin >> created.posX; | |
| if (created.posX < 0) break; | |
| std::cin.ignore(); // Skip the comma. | |
| std::cin >> created.posY; | |
| if (created.posY < 0) break; | |
| if (sx < created.posX) sx = created.posX; | |
| if (sy < created.posY) sy = created.posY; | |
| points.push_back(created); | |
| } | |
| std::cout << "Field size: " << sx << "x" << sy << std::endl; | |
| for (size_t y = 0; y <= sy; y++) { | |
| for (size_t x = 0; x <= sx; x++) { | |
| int min = sx+sy+2; | |
| Point *closest = nullptr; | |
| bool biggestDistEq = false; | |
| for (auto &point : points) { | |
| int dist = abs(point.posX - x) + abs(point.posY - y); | |
| if (dist < min){ | |
| closest = &point; | |
| min = dist; | |
| biggestDistEq = false; | |
| } else if (dist == min) { | |
| biggestDistEq = true; | |
| } | |
| } | |
| if (biggestDistEq == true) closest = nullptr; | |
| if (closest != nullptr) { | |
| if (x == 0 || x == sx || y == 0 || y == sy) closest->infinite == true; | |
| closest->size++; | |
| if (closest->posX == x && closest->posY == y) { | |
| std::cout << "X\t"; | |
| } else { | |
| std::cout << (*closest).name << '\t'; | |
| } | |
| } else { | |
| std::cout << ".\t"; | |
| } | |
| } | |
| std::cout << std::endl; | |
| } | |
| Point *biggest = &points[0]; | |
| for(Point &point : points) { | |
| if (!point.infinite && point.size > (*biggest).size) { | |
| biggest = &point; | |
| } | |
| } | |
| std::cout << "Point " << biggest->name << " at (" << biggest->posX << ", " << biggest->posY << ") covers the biggest area: " << biggest->size << std::endl; | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <map> | |
| #include <vector> | |
| #include <utility> | |
| #include <cmath> | |
| #include <cstdlib> | |
| typedef struct { | |
| int posX, posY; | |
| int name; | |
| int size = 0; | |
| bool infinite = false; | |
| } Point; | |
| int main(int argc, char const *argv[]) { | |
| std::vector<Point> points; | |
| int sx = 0, sy = 0, pc = 1; | |
| while (true) { | |
| Point created; | |
| created.name = pc++; | |
| std::cout << "Enter X & Y of point " << created.name << ": "; | |
| std::cin >> created.posX; | |
| if (created.posX < 0) break; | |
| std::cin.ignore(); // Skip the comma. | |
| std::cin >> created.posY; | |
| if (created.posY < 0) break; | |
| if (sx < created.posX) sx = created.posX; | |
| if (sy < created.posY) sy = created.posY; | |
| points.push_back(created); | |
| } | |
| std::cout << "Field size: " << sx << "x" << sy << std::endl; | |
| for (size_t y = 0; y <= sy; y++) { | |
| for (size_t x = 0; x <= sx; x++) { | |
| int min = (int) sqrt(sx*sx + sy*sy); | |
| Point *closest = nullptr; | |
| bool biggestDistEq = false; | |
| for (auto &point : points) { | |
| int dist = (int) sqrt(pow(1.0 * point.posX - x, 2) + pow(1.0 * point.posY - y, 2)); | |
| if (dist < min){ | |
| closest = &point; | |
| min = dist; | |
| biggestDistEq = false; | |
| } else if (dist == min) { | |
| biggestDistEq = true; | |
| } | |
| } | |
| if (biggestDistEq == true) closest = nullptr; | |
| if (closest != nullptr) { | |
| if (x == 0 || x == sx || y == 0 || y == sy) closest->infinite == true; | |
| closest->size++; | |
| if (closest->posX == x && closest->posY == y) { | |
| std::cout << "X\t"; | |
| } else { | |
| std::cout << (*closest).name << '\t'; | |
| } | |
| } else { | |
| std::cout << ".\t"; | |
| } | |
| } | |
| std::cout << std::endl; | |
| } | |
| Point *biggest = &points[0]; | |
| for(Point &point : points) { | |
| if (!point.infinite && point.size > (*biggest).size) { | |
| biggest = &point; | |
| } | |
| } | |
| std::cout << "Point " << biggest->name << " at (" << biggest->posX << ", " << biggest->posY << ") covers the biggest area: " << biggest->size << std::endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment