题意:
给出 6 组宽高,判断这六组宽高的长方形能否拼成一个长方体。
思路:
6 个矩形能够拼成一个长方体,则它的边的长度可能有三种:
有 3 种边长:一般的长方体
有 2 种边长:底面为正方形的长方体
有 1 种边长:正方体
有 3 种边长时,假设这 3 种边长分别为 a,b,c,则长方体 6 个侧面的宽高是 2 个 (a,b),2 个 (a,c),2 个 (b,c)(宽高的左右顺序可以调换)
有 2 种边长时,假设这 2 种边长分别为 a,b,则长方体 6 个侧面的宽高是 2 个 (a,a) 和 4 个 (a,b) 或 2 个 (b,b) 和 4 个 (a,b)(宽高的左右顺序可以调换)
有 1 种边长时,假设这 1 种边长为 a,则长方体 6 个侧面的宽高是 6 个 (a,a)
这样分析下来,我们可以在读取数据的过程中用 map 记录下当前长度是否出现过,并且可以给长度编号,这样我们就知道出现了几个长度了。然后再数组记录下对应编号的宽高出现的次数。最后按照长度的个数进行比较判断即可。
下面对两个样例进行讲解:
3 个长度,t[0][1]+t[1][0]==2,t[0][2]+t[2][0]==2,t[1][2]+t[2][1]==2,所以 POSSIBLE。
4 个长度,所以 IMPOSSIBLE。
注意:
暂无
代码:
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<stack>
#include<queue>
#include<utility>
#include<vector>
#include<cmath>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long LL;
int num;
map<int, int> mp;
int w, h;
int t[10][10];
bool Input()
{
num = 0;
mp.clear();
memset(t, 0, sizeof(t));
for(int i=0; i<6; i++){
if(scanf("%d%d", &w, &h) != 2) return false;
if(mp.find(w) == mp.end()) mp[w] = num++;
if(mp.find(h) == mp.end()) mp[h] = num++;
t[mp[w]][mp[h]]++;
}
return true;
}
int main()
{
//freopen("in.txt", "r", stdin);
while(Input()){
if(num==1 && t[0][0]==6){
printf("POSSIBLE\n");
}else if(num==2 && (t[0][0]==2|| t[1][1]==2) && t[0][1]+t[1][0]==4){
printf("POSSIBLE\n");
}else if(num==3 && t[0][1]+t[1][0]==2 && t[0][2]+t[2][0]==2 && t[1][2]+t[2][1]==2){
printf("POSSIBLE\n");
}else{
printf("IMPOSSIBLE\n");
}
}
return 0;
}
想看书上的源代码的话看这 (^▽^)
https://github.com/aoapc-book/aoapc-bac2nd