UVa1587:给定6个矩形的长和宽w i 和h i (1≤w i ,h i ≤1000),判断它们能否构成长方体的6个面。

思路:先通过枚举确定这六组长宽对是两两相同的。在判定过程中,将对应的(去重后的)的长宽对记录下来形成由(a,b)(b,c)(c,a)组成的数组log。然后log[0]和log[1]设为a和b,于是确定2~3之间有一个a或者b,对应的4~5有b或者a,并且记录它们的位置为i,j。最后,枚举2~5把位置不为i和j的元素(两个)填充到log[0]和log[1]中,仅仅当二者相同时,才可以认为possible。

#include<stdio.h>
//#include<time.h>

int recs[6][2];//recs[i][0] is i'th rectangle's width and recs[i][1] is its height
int log[7];
int logptr;
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	
	while(EOF != scanf("%d%d", &recs[0][0], &recs[0][1])){
		//input
		for(int i = 1; i < 6; i++)
			scanf("%d%d", &recs[i][0], &recs[i][1]);
		//compare and judge, 3 times all told, and after every judge we decrease the compare range
		int i;
		logptr = 0;
		for(i = 0; i < 3; i++){
			int j;
			for(j = i + 1; j < 6 - i; j++){
				if((recs[j][0] == recs[i][0] && recs[j][1] == recs[i][1]) || (recs[j][0] == recs[i][1] && recs[j][1] == recs[i][0])){
					//find one the same
					//swap with the last rec to decrease the range
					recs[j][0] ^= recs[5 - i][0];//width
					recs[5 - i][0] ^= recs[j][0];
					recs[j][0] ^= recs[5 - i][0];
					
					recs[j][1] ^= recs[5 - i][1];//height
					recs[5 - i][1] ^= recs[j][1];
					recs[j][1] ^= recs[5 - i][1];
					//log
					log[logptr++] = recs[i][0];
					log[logptr++] = recs[i][1];
					break;
				}
			}
			if(j == 6 - i) break;
		}
		if(i == 3) {
			//make sure it's (ab)(bc)(ca), in this (xy) means xy or yx
			//negative example:3,3,3,2,2,2
			int i, j = 0;
			for(i = 2; i < 4; i++){
				if(log[i] == log[0] || log[i] == log[1]){
					j = i;
					if(log[i] == log[0]) log[0] = log[1];
					else log[1] = log[0];
					break;
				}
			}
			if(i == 4){
				printf("IMPOSSIBLE\n");
				continue;
			}
			for(i = 4; i < 6; i++){
				if(log[i] == log[0]){
					break;
				}
			}
			if(i == 6) {
				printf("IMPOSSIBLE\n");
				continue;
			}
			logptr = 0;
			for(int k = 2; k < 6; k++){
				if(k != i && k != j){
					log[logptr++] = log[k];
				}
			}
			if(log[0] == log[1]){
				printf("POSSIBLE\n");
			}
			else printf("IMPOSSIBLE\n");
		}
		else printf("IMPOSSIBLE\n");
	}
	
	//printf("Total time: %.4f s.", (double)clock()/CLOCKS_PER_SEC); 
	return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值