UVa 591 Box(盒子)

Description

Download as PDF
Ivan works at a factory that produces heavy machinery. He has a simple job -- he knocks up wooden boxes of different sizes to pack machinery for delivery to the customers. Each box is a rectangular parallelepiped. Ivan uses six rectangular wooden pallets to make a box. Each pallet is used for one side of the box.
\epsfbox{p3214.eps}
Joe delivers pallets for Ivan. Joe is not very smart and often makes mistakes -- he brings Ivan pallets that do not fit together to make a box. But Joe does not trust Ivan. It always takes a lot of time to explain Joe that he has made a mistake. Fortunately, Joe adores everything related to computers and sincerely believes that computers never make mistakes. Ivan has decided to use this for his own advantage. Ivan asks you to write a program that given sizes of six rectangular pallets tells whether it is possible to make a box out of them.

Input 

Input file contains several test cases. Each of them consists of six lines. Each line describes one pallet and contains two integer numbers w and  h (  1$ \le$wh$ \le$10 000) -- width and height of the pallet in millimeters respectively.

Output 

For each test case, print one output line. Write a single word `  POSSIBLE' to the output file if it is possible to make a box using six given pallets for its sides. Write a single word `  IMPOSSIBLE' if it is not possible to do so.

Sample Input 

1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683
1234 4567
1234 4567
4567 4321
4322 4567
4321 1234
4321 1234

Sample Output 

POSSIBLE
IMPOSSIBLE
	
	

这道题是一道很简单的立体几何题,只要把条件考虑周全了,就很容易过。此题先创建了一个长方形结构体,重载了流输入和小于运算符,方便输入和用sort排序,重载了!=运算符,方便比较。当然不重载也可以,手动排序无可厚非,就是代码会长一点~

这题的思路是先给这六个矩形排一下序,依据长和宽的大小(输入的时候保证了h>=w),如果可以组成立方体,那么前两个,中间两个,后两个(排序后)一定是相等的两个矩形,如果可以组成立方体,那么第0、2、4(有公共顶点的三个面)这三个矩形一定是其中一个的宽和长分别等于另外两个的宽和长,只要其中一个条件不满足,它就不能组成立方体,否则就可以,所以很容易用或逻辑复合一个条件来解决。代码如下:

#include 
#include 
#include 

using namespace std;

struct rect
{
	int w;
	int h;
	friend istream& operator >> (istream&,rect&);
	bool operator != (const rect a)const	//重载!=运算,方便后面写条件
	{
		return !(a.h==h&&a.w==w);
	}
	bool operator < (const rect a)const		//重载<运算符,使它可以用sort排序
	{
		if(w!=a.w)
			return w> (istream & input,rect & a)	//重载流输入,方便输入矩形的数据
{
	input>>a.w>>a.h;
	if(a.w>a.h)
		swap(a.w,a.h);		//把长宽弄成固定形式,即始终h>=w
	return input;
}

int main()
{
	int i;
	while(cin>>r[0])
	{
		for(i=1;i<6;i++)
			cin>>r[i];
		sort(r,r+6);
		if(r[0]!=r[1]||r[2]!=r[3]||r[4]!=r[5]||r[0].w!=r[2].w||r[0].h!=r[4].w||r[2].h!=r[4].h)	//这个条件的组合使代码量骤降,认真体会就能明白
			cout<<"IMPOSSIBLE"<


你可能感兴趣的:(UVa,Online,Judge)