uva 1587 Box(思路)

给6个矩形的长和宽(或者宽和长),问这六个矩形能否组成一个长方体.

思路比较简单,不过需要注意的地方有点多.

首先由于长和宽的顺序为止,所以要处理一下(一开始只处理了后来读入的五组,没有处理单独读入的第一组,差评)

然后要判断能否分成两两相同的三组.

如果能,枚举8种可能的相等的情况.

  1 /*************************************************************************
  2     > File Name: code/uva/1587.cpp
  3     > Author: 111qqz
  4     > Email: [email protected]
  5     > Created Time: 2015年09月22日 星期二 12时20分58秒
  6  ************************************************************************/
  7
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21 #define y1 hust111qqz
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 #define lr dying111qqz
 26 using namespace std;
 27 #define For(i, n) for (int i=0;i<int(n);++i)
 28 typedef long long LL;
 29 typedef double DB;
 30 const int inf = 0x3f3f3f3f;
 31
 32 struct Q
 33 {
 34     int w,h;
 35 }a,b,c,q[10];
 36 bool ok ( int i)
 37 {
 38
 39     if (q[i].w==q[i+1].w&&q[i].h==q[i+1].h) return true;
 40     return false;
 41 }
 42
 43 bool solve ()
 44 {
 45     if (a.w==b.w&&a.h==c.h&&b.h==c.w) return true;
 46     if (a.w==b.h&&a.h==c.h&&b.w==c.w) return true;
 47     if (a.w==b.w&&a.h==c.w&&b.h==c.h) return true;
 48     if (a.w==b.h&&a.h==c.w&&b.w==c.h) return true;
 49
 50     if (a.w==c.w&&a.h==b.h&&b.w==c.h) return true;
 51     if (a.w==c.w&&a.h==b.w&&b.h==c.h) return true;
 52     if (a.w==c.h&&a.h==b.w&&b.h==c.w) return true;
 53     if (a.w==c.h&&a.h==b.h&&b.w==c.w) return true;
 54     return false;
 55 }
 56
 57 bool cmp(Q a,Q b)
 58 {
 59     if (a.w<b.w) return true;
 60     if (a.w==b.w&&a.h<b.h) return true;
 61     return false;
 62 }
 63 int main()
 64 {
 65   #ifndef  ONLINE_JUDGE
 66    freopen("in.txt","r",stdin);
 67   #endif
 68
 69    while (scanf("%d %d",&q[0].w,&q[0].h)!=EOF)
 70     {
 71     if (q[0].w>q[0].h) swap(q[0].w,q[0].h);
 72     for ( int i = 1 ; i < 6 ; i++)
 73     {
 74        // scanf("%d %d",&w[i],&h[i]);           //蠢了..开始开了两个数组读的长和宽.排序后对应关系就打乱了233
 75         scanf("%d %d",&q[i].w,&q[i].h);
 76         if (q[i].w>q[i].h) swap(q[i].w,q[i].h);
 77     }
 78
 79     bool flag = true;
 80     sort(q,q+6,cmp);
 81 //    for ( int i = 0 ; i < 6 ; i++) cout<<q[i].w<<" "<<q[i].h<<endl;
 82     for ( int i = 0 ; i < 6 ; i = i + 2)
 83     {
 84         if (!ok(i))
 85         {
 86         flag = false;
 87         break;
 88         }
 89     }
 90     if (!flag)
 91     {
 92         puts("IMPOSSIBLE");
 93         continue;
 94     }
 95     a.w = q[0].w; a.h=q[0].h;
 96     b.w = q[2].w; b.h=q[2].h;
 97     c.w = q[4].w; c.h=q[4].h;
 98 //    cout<<a.w<<" "<<a.h<<endl;
 99 //    cout<<b.w<<" "<<b.h<<endl;
100 //    cout<<c.w<<" "<<c.h<<endl;
101
102     if (solve())
103     {
104         puts("POSSIBLE");
105     }
106     else
107     {
108         puts("IMPOSSIBLE");
109     }
110
111     }
112
113
114  #ifndef ONLINE_JUDGE
115   fclose(stdin);
116   #endif
117     return 0;
118 }

时间: 2024-08-09 19:52:46

uva 1587 Box(思路)的相关文章

UVa 1587 Box 盒子

这道题的链接:https://vjudge.net/problem/UVALive-3214 给定6个矩形的长和宽wi与hi(1<=wi, hi<=1000), 判断它们能否构成长方体的6个面 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

UVa 1587 Box

大水题一发  弄清长方体的几个面的关系就行了 #include<cstdio> #include<algorithm> using namespace std; const int N = 6; struct rec{ int l, w;} r[N]; bool cmp(rec a, rec b) { return a.w < b.w || (a.w == b.w && a.l < b.l); } int main() { int a, b, ok; w

uva 1587(Box UVA - 1587)

题目大意是给定6个数对,每个数对代表一个面的长和宽,判断这6个面是否能构成一个长方体. 这种题一看很复杂,但是只要不想多了实际上这就是一个水题... 首先说明一下判断的思路: 1.长方体是有三个对面的,所以先把这三个对面找出来(因为输入的长和宽是不确定的,所以先把每一组输入的两个数按照从大到小进行调整(这里建议开一个结构体数组)).调整完之后,自己写一个cmp的函数用来sort,cmp是先比较长(按照升序或者降序是无所谓的,随你)然后如果长相等那么就比较宽(按照和长一样的顺序比较). 2.你以为

Uva 1587 - Box ( 思维 )

题意: 给定6个矩形的长和宽wi和hi(1<=wi,hi<=1000),判断它们能否构成长方形的6个面. 脑洞打开~~ /* 1 1 1 1 2 2 2 2 3 3 3 3 */ 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<cmath> 7 #

uva 12293 - Box Game(组合游戏)

题目链接:uva 12293 - Box Game 题目大意:有两个盒子,第一个盒子装有n个球,第二个盒子装又1个球,每次操作将少的盒子中的球全部拿掉,并从另一个盒子中取一些球放入该盒子,不能使另一个盒子中球的个数为0.两人轮流操作,问说最后谁胜. 解题思路:n如果为2i?1那么先手必败. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; bool judge (

UVA 12293 - Box Game(博弈)

UVA 12293 - Box Game 题目链接 题意:两个盒子,一开始一个盒子有n个球,一个只有1个球,每次把球少的盒子中球消掉,把多的拿一些球给这个盒子,最后不能操作的输(球不能少于1个),Alice先手,问谁赢 思路:博弈,题目其实可以转化为,给定一个n,每次把减少1到n/2的数字,最后谁是1谁就输了,那么可以去递推前几项找个规律,或者推理,都可以发现只要是2^i - 1的数字Bob就赢,否则Alice赢 代码: #include <stdio.h> #include <stri

UVA - 12293 Box Game (规律)

Description  Box Game  There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves alternatively, Alice mo

UVa 1587 有趣的模拟题

背景:这道题ac的时候我是不由自主的仰天长啸啊!!一直WA,WA了十多个小时,我真的不知道错在哪里了,索性搜 索别人的解题报告.最后最主要的是借鉴了排序思想. 思路:先将六组数据在横向进行排序,大的在左边,然后就对相同的数据进行合并,如果可以组成长方体,那么六组rectangular合并之后一定是三组.对于这三组数据进行纵向 的结构体排序,x大的靠上,如果x相等那么就以看y,y大的考上.这样排序之后就得到这样的关系: 然后根据简单的数学证明就可以得到:只有X1==X2&&y2==y3&am

UVA 12293 Box Game(博弈入门)

题目链接:Box Game 题面:            12293 Box Game There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves al