UVA 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.


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 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

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

Sample Output

POSSIBLE

Source

题意:就是给你六个面,让你判断能不能组成长方体。
思路:题目有点恶心,判断条件较多啊,所以要分清各类情况。首先要知道长方体12 条边中最多有三个不同的边;
  1.如果边数大于3 直接输出 IMPOSSIBLE
  2.如果边数等于1,就输出POSSIBLE
  3.如果边数等于2,判断所给的六个面,与自己判断得出的六个面相符合,符合POSSIBLE;不符合IMPOSSIBLE;
  4.如果边数等于3,判断所给的六个面,与自己判断得出的六个面相符合,符合POSSINLE;不符合IMPOSSIBLE;
看起来很简单,但WA了好多次;菜的要死啊,继续努力,不放弃,加油。。。。
AC 代码:
#include<stdio.h>
#include<math.h>
int main(){
    int a[12];//存储12 条边;
    int sum;//统计不同边的个数;
    int b[3];//不同边的值
    int i,j,tag1,tag2,tag;
    int c[6][2];//不同边组成的六个面
    while(scanf("%d",&a[0])!=EOF){
           sum=1;
           tag=0;
           b[0]=a[0];
        for(i=1;i<12;i++){
            scanf("%d",&a[i]);
            for(j=0;j<i;j++){
                if(a[i]==a[j]){
                    break;
                }
            }
            if(j==i){
                if(sum<3){
                    b[sum]=a[i];
                }
                sum++;
            }
        }
        if(sum>3){printf("IMPOSSIBLE\n");continue;}//边大于3
        if(sum==1){  printf("POSSIBLE\n");continue;}//边等于1
        if(sum==2){//边等于2
            tag1=0;
            tag2=0;
            tag=0;
            for(i=0;i<12;i++){
                if(a[i]==b[0]){
                    tag1++;
                }
                if(a[i]==b[1]){
                    tag2++;
                }
            }//找出记录两条边各自相同的数目
            if(tag1>tag2){//说明记录的值个数b[0]>b[1];下面是够成的六个面;
                    c[0][0]=b[0];c[0][1]=b[1];
                    c[1][0]=b[0];c[1][1]=b[1];
                    c[2][0]=b[0];c[2][1]=b[1];
                    c[3][0]=b[0];c[3][1]=b[1];
                    c[4][0]=b[0];c[4][1]=b[0];
                    c[5][0]=b[0];c[5][1]=b[0];
                            }
            if(tag1<tag2){//b[1]>b[0]
                    c[0][0]=b[1];c[0][1]=b[1];
                    c[1][0]=b[1];c[1][1]=b[1];
                    c[2][0]=b[0];c[2][1]=b[1];
                    c[3][0]=b[0];c[3][1]=b[1];
                    c[4][0]=b[0];c[4][1]=b[1];
                    c[5][0]=b[0];c[5][1]=b[1];
                            }
            for(i=0;i<6;i++){//自己组合的边与所给边对比,并把比过相等的置0;
                    for(j=0;j<12;j+=2){
                        if((a[j]==c[i][0]&&a[j+1]==c[i][1])||(a[j+1]==c[i][0]&&a[j]==c[i][1])){
                            a[j]=0;
                            a[j+1]=0;
                            break;
                        }
                    }
                }
            for(j=0;j<12;j+=2){
                    if(a[j]!=0){
                        tag=1;
                        break;
                    }
             }
             if(tag==0){
                 printf("POSSIBLE\n");continue;
              }
                 printf("IMPOSSIBLE\n");continue;
        }
        if(sum==3){
                c[0][0]=b[0];c[0][1]=b[1];
                c[1][0]=b[0];c[1][1]=b[2];
                c[2][0]=b[1];c[2][1]=b[0];
                c[3][0]=b[1];c[3][1]=b[2];
                c[4][0]=b[2];c[4][1]=b[0];
                c[5][0]=b[2];c[5][1]=b[1];
                for(i=0;i<6;i++){
                    for(j=0;j<12;j+=2){
                        if((a[j]==c[i][0]&&a[j+1]==c[i][1])||(a[j+1]==c[i][0]&&a[j]==c[i][1])){
                            a[j]=0;
                            a[j+1]=0;
                            break;
                        }
                    }
                }
            for(j=0;j<12;j+=2){
                    if(a[j]!=0){
                            tag=1;
                        break;
                    }
            }
            if(tag==1){printf("IMPOSSIBLE\n");continue;}
            printf("POSSIBLE\n");continue;
        }
    }
return 0;
}
下面介绍以面为基础的做法:
思路:先对面的两条边进行排序(升序);最后找到相同的面用书组记录这两个相同的面,最后得到三个面,进行排序,使得第一个面的两个边长为三个数中最小与第二小;然后进行判断即可;这样代码减少好多,以后做题多想想,思路清晰在敲吧。。。。。。。。
AC代码:
#include<stdio.h>
#include<string.h>
int main()
{
  int t,i,j;
  int a[6][2],b[3][2];
  while(scanf("%d%d",&a[0][0],&a[0][1])!=EOF){
    if(a[0][0]>a[0][1]){t=a[0][0];a[0][0]=a[0][1];a[0][1]=t;}
    for(i=1;i<6;i++){
        scanf("%d%d",&a[i][0],&a[i][1]);
        if(a[i][0]>a[i][1]){t=a[i][0];a[i][0]=a[i][1];a[i][1]=t;}
    }
    t=0;
    for(i=0;i<5;i++){
       for(j=i+1;j<6;j++){
        if(a[i][0]==a[j][0]&&a[i][1]==a[j][1]&&a[i][0]!=0){
            b[t][0]=a[i][0];
            b[t][1]=a[i][1];
            a[i][0]=0;
            a[i][1]=0;
            a[j][0]=0;
            a[j][1]=0;
            t++;
            break;
        }
       }
    }
    if(t==3){
            t=0;
        for(i=1;i<3;i++){
             if(b[0][0]>b[i][0]){
                 t=b[0][0];b[0][0]=b[i][0];b[i][0]=t;
                 t=b[0][1];b[0][1]=b[i][1];b[i][1]=t;
                 }
             if(b[0][0]==b[i][0]&&b[0][1]>b[i][1]){
                 t=b[0][1];b[0][1]=b[i][1];b[i][1]=t;
                 }
        }
        if(b[0][1]==b[1][0]&&b[2][0]==b[0][0]&&b[2][1]==b[1][1]){t=1;}
        if(b[0][1]==b[2][0]&&b[1][0]==b[0][0]&&b[1][1]==b[2][1]){t=1;}
        if(t==1){printf("POSSIBLE\n");}
        else{printf("IMPOSSIBLE\n");}
    }
    else{
        printf("IMPOSSIBLE\n");
    }
  }
  return 0;
}
这是道水题卡了差不多一天,还是很菜,依然继续努力;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值