Box UVA - 1587

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.
在这里插入图片描述

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 ≤ w, h ≤ 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

HINT

题目的意思是给我们六个矩形的长和宽,让我们判断能否构成一个长方体。考虑长方形的特点可知,总会有两个面的长和宽hi相等的,同时12个边中总会又出现至少每4个边是相等的。可以根据这两个条件来判断输入样例。

Accepted

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int cmp(const void* a, const void* b)
{
	return *(int*)a - *(int*)b;
}

int main()
{
	int a, b;
	while (scanf("%d %d", &a, &b) != EOF)
	{
		int arr[12] = { 0 };
		int arr2[6][2] = { 0 };
		arr2[0][0] = a > b ? a : b;
		arr2[0][1] = a > b ? b : a;
		int j = 2;
		arr[0] = a;arr[1] = b;
		for (int i = 1;i < 6;i++)
		{
			scanf("%d %d", &a, &b);
			arr2[i][0] = a > b ? a : b;
			arr2[i][1] = a > b ? b : a;
			arr[j++] = a;arr[j++] = b;
		}
		qsort(arr, 12, sizeof(int), cmp);
		int flag = 0;
		for (int i = 0;i < 12;i += 4)
			for (int j = 0;j < 4;j++)
				if (arr[i] != arr[j + i])
					flag = 1;
		if (!flag)
		{
			for (int i = 0;i < 6;i++)
			{
				int j;
				if (!arr2[i][0])continue;
				for (j = 0;j < 6;j++)
					if (i != j && arr2[i][0] == arr2[j][0] && arr2[i][1] == arr2[j][1])
					{
						arr2[i][0] = arr2[j][0] = arr2[i][1] = arr2[j][1] = 0;
						break;
					}
				if (j == 6)
				{
					flag = 1;
					break;
				}
			}				
		}
		printf("%s\n", flag == 0 ? "POSSIBLE" : "IMPOSSIBLE");
	}
}
posted @ 2021-01-31 17:25  布拉多1024  阅读(49)  评论(0)    收藏  举报