Uva1587

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 ≤ 10000) — 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

 1 #include<bits/stdc++.h>
 2 #define maxn 100010
 3 using namespace std;
 4 int main()
 5 {
 6     int w[6],h[6];
 7     while(cin>>w[0]>>h[0]>>w[1]>>h[1]>>w[2]>>h[2]>>w[3]>>h[3]>>w[4]>>h[4]>>w[5]>>h[5])
 8     {
 9         set<int>a;
10         for(int i=0;i<6;i++)
11         {
12             a.insert(w[i]);
13             a.insert(h[i]);
14         }
15         int len=a.size();
16         if(len==1)printf("POSSIBLE
");
17         else if(len==2)
18         {
19             int sum=0;
20             for(int i=0;i<=5;i++)
21             {
22                 if(w[i]==h[i])
23                 {
24                     sum++;
25                 }
26             }
27             if(sum==2)printf("POSSIBLE
");
28             else printf("IMPOSSIBLE
");
29         }
30         else if(len==3)
31         {
32             int flag=0;
33             int k[3],j=0;
34             for(set<int>::iterator it=a.begin();it!=a.end();it++)
35             {
36                 k[j++]=*it;
37             }
38             int k1=0,j1=0,l1=0;
39             for(int i=0;i<=5;i++)
40             {
41                 if(w[i]==h[i])
42                 {
43                     flag=1;break;
44                 }
45                 if(k[0]==w[i]||k[0]==h[i])k1++;
46                 if(k[1]==w[i]||k[1]==h[i])j1++;
47                 if(k[2]==w[i]||k[2]==h[i])l1++;
48             }
49             if(k1!=4||j1!=4||l1!=4)flag=1;
50             if(!flag)printf("POSSIBLE
");
51             else printf("IMPOSSIBLE
");
52         }
53         else
54             printf("IMPOSSIBLE
");
55     }
56     return 0;
57 }

思路:

判断输入进的每组数中有几个不同的值。

1个值 必定成立。

2个值 如果出现两次w[i]==h[i]成立否则不成立。

3个值 必须保证每个值出现4次且h[i]!=w[i],第一次做WA就在这里,没有考率到 4 5 ,4 5, 3 4,3 4,3 4,3 5这样的错误情况,只考虑了h[i]!=w[i]。

4个值及以上必定不成立。

原文地址:https://www.cnblogs.com/zuiaimiusi/p/10941203.html