[算法竞赛入门经典]Box,ACM/ICPC NEERC 2004,UVa1587(水题)

Description

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

其实挺简单的,我用了结构体来存放每个矩形,并在矩形初始化的时候将较大的边设为x边
之后利用sort将结构体数组排序,例如:

1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683

排序后:

2584 1345
2584 1345
2584 683
2584 683
1345 683
1345 683

看出规律了吗?

Code

#include <algorithm>
#include <cstdio>
#define MAX 1005
using namespace std;
typedef struct Rectangle{
    int x,y;
    Rectangle(int a,int b){
        if(a>b) { x=a; y=b; }
        else { x=b; y=a; }
    }
}Rect;
Rect* rects[6];
bool cmp(Rect* a,Rect* b){
    if(a->x == b->x){ return (a->y) > (b->y); }
    return (a->x) > (b->x);
}

int main()
{
    int a,b,flag;
    while(~scanf("%d%d",&a,&b)&& a && b){
        bool ok = true;
        rects[0] = new Rect(a,b);
        for(int i = 1;i < 6;i ++){
            scanf("%d%d",&a,&b);
            rects[i] = new Rect(a,b);
        }
        sort(rects,rects+6,cmp);
        flag = rects[0]->x;
        for(int i = 1;i < 4;i ++) if(rects[i]->x != flag) { ok=false; break; }
        flag = rects[2]->y;
        for(int i = 3;i < 6;i ++) if(rects[i]->y != flag) { ok=false; break; }
        flag = rects[0]->y;
        if(!((rects[1]->y==flag)&&(rects[4]->x==flag)&&(rects[5]->x==flag))) ok = false;
        if(ok) printf("POSSIBLE\n");
        else printf("IMPOSSIBLE\n");
        for(int i = 0;i < 6;i ++) delete rects[i];      // 删除每个矩形
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/1Kasshole/p/9690079.html
今日推荐