Skip to content

Instantly share code, notes, and snippets.

@WindAzure
Last active September 26, 2018 16:17
Show Gist options
  • Select an option

  • Save WindAzure/3764754bb71e025becf7a79a77cfaccd to your computer and use it in GitHub Desktop.

Select an option

Save WindAzure/3764754bb71e025becf7a79a77cfaccd to your computer and use it in GitHub Desktop.
UVa 1587
#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 &currentRectangle, 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