Last active
September 26, 2018 16:17
-
-
Save WindAzure/3764754bb71e025becf7a79a77cfaccd to your computer and use it in GitHub Desktop.
UVa 1587
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 <stdio.h> | |
| #include <vector> | |
| using namespace std; | |
| class Rectangle | |
| { | |
| public: | |
| int width = 0; | |
| int height = 0; | |
| int appearTimes = 1; | |
| Rectangle() {} | |
| Rectangle(int w, int h) { width = w, height = h; } | |
| }; | |
| bool IsSameRectangle(Rectangle &rectangle1, Rectangle &rectangle2) | |
| { | |
| return (rectangle1.width == rectangle2.width && rectangle1.height == rectangle2.height) || | |
| (rectangle1.height == rectangle2.width && rectangle1.width == rectangle2.height); | |
| } | |
| bool IsAbleAddNewRectangle(Rectangle ¤tRectangle, std::vector<Rectangle> &rectangles) | |
| { | |
| for (auto &rectangle : rectangles) | |
| { | |
| if (rectangle.appearTimes == 2) | |
| { | |
| continue; | |
| } | |
| if (IsSameRectangle(rectangle, currentRectangle)) | |
| { | |
| rectangle.appearTimes++; | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| bool FitTwoRectangles(Rectangle &topRectangle, Rectangle &rightRectangle, Rectangle forwardRectangle) | |
| { | |
| if (topRectangle.width == rightRectangle.width) | |
| { | |
| auto estimateForwardRectangle = Rectangle(rightRectangle.height, topRectangle.height); | |
| return IsSameRectangle(estimateForwardRectangle, forwardRectangle); | |
| } | |
| else if (topRectangle.width == rightRectangle.height) | |
| { | |
| auto estimateForwardRectangle = Rectangle(rightRectangle.width, topRectangle.height); | |
| return IsSameRectangle(estimateForwardRectangle, forwardRectangle); | |
| } | |
| return false; | |
| } | |
| bool IsAllRectangleAbleMakeBox(vector<Rectangle> &rectangles) | |
| { | |
| if (rectangles.size() != 3) | |
| { | |
| return false; | |
| } | |
| auto topRectangle = rectangles[0]; | |
| auto rightRectangle = rectangles[1]; | |
| auto forwardRectangle = rectangles[2]; | |
| return FitTwoRectangles(topRectangle, rightRectangle, forwardRectangle) || | |
| FitTwoRectangles(topRectangle, forwardRectangle, rightRectangle); | |
| } | |
| int main() | |
| { | |
| auto T = 0; | |
| auto rectangle = Rectangle{}; | |
| auto rectangles = vector<Rectangle>{}; | |
| while (~scanf("%d%d", &rectangle.width, &rectangle.height)) | |
| { | |
| T++; | |
| if (IsAbleAddNewRectangle(rectangle, rectangles)) | |
| { | |
| rectangles.push_back(rectangle); | |
| } | |
| if (T == 6) | |
| { | |
| T = 0; | |
| printf("%s\n", IsAllRectangleAbleMakeBox(rectangles) ? "POSSIBLE" : "IMPOSSIBLE"); | |
| rectangles.clear(); | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment