【白书之路】 Uva 1587 Box 六个面是否能组成长方体

1587 Box

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.
【白书之路】 Uva 1587 Box 六个面是否能组成长方体_第1张图片

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


给出6个面的长和宽,判断这6个面是否能组成一个长方体。

1.需要满足两个条件:

    (1)存在三组两两相同的对面

    (2)对于三个不同的面,任何两个面之间至少存在一条公共边

2.对于条件1,首先对6个面进行排序,如果满足条件,则相邻的两个面一定是相同的

3.对于条件2,我们取出三个不相同的面,然后使用三种组合进行判断两个面之间是否有公共边


#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <memory.h>
#include <stdlib.h>
#include <map>
using namespace std;

struct Pallet//记录每个面的信息
{
    int height;
    int width;
};

int temp1,temp2;//输入的临时变量

Pallet pallet[6];//6个面
int com[3][2]={0,2,0,4,2,4};//三种比较组合0与2,0与4,2与4
map<int,int> appear;//判断某个数字是否出现过

bool cmp(const Pallet& a,const Pallet& b)//以height为第一比较,width为第二比较
{
    if(a.height<b.height)
        return true;
    else if(a.height==b.height&&a.width<b.width)
        return true;
    return false;
}

bool judge1()//判断是否有三组相等的面,使用内存字节比较
{
    for(int i=0;i<6;i+=2)
    {
        if(memcmp(&pallet[i],&pallet[i+1],sizeof(Pallet))!=0)
            return false;
    }
    return true;
}

bool judge2()//判断任意两个不同的面是否存在至少一条公共边
{
    bool flag=true;

    for(int i=0;i<3;i++)//三种组合
    {
        appear.clear();
        appear[pallet[com[i][0]].height]=1;//标记一个面的两条边
        appear[pallet[com[i][0]].width]=1;
        if(appear[pallet[com[i][1]].height]==0&&appear[pallet[com[i][1]].width]==0)//看是否出现过
        {
            flag=false;
            break;
        }
    }
    return flag;
}

int main()
{
    int i;
    while(~scanf("%d%d",&temp1,&temp2))//遇到EOF结束,预输入
    {
        pallet[0].height=max(temp1,temp2);//强制让width小于height
        pallet[0].width=min(temp1,temp2);
        for(i=1;i<6;i++)
        {
            scanf("%d%d",&temp1,&temp2);
            pallet[i].height=max(temp1,temp2);
            pallet[i].width=min(temp1,temp2);
        }
        sort(pallet,pallet+6,cmp);//排序
        if(judge1()&&judge2())//判断两种情况
        {
            printf("POSSIBLE\n");
        }
        else
        {
            printf("IMPOSSIBLE\n");
        }
    }
    return 0;
}



你可能感兴趣的:(box,uva,白书之路,六个面是否能组成长方体,1587)