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
//#include

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



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