算法竞赛入门经典 习题3-10

UVa1587

Box

参考了一下其它博主的思路:

  • 交换每个长方形的宽和高,使宽小于高
  • 对6个长方形进行排序,使宽小的排在前面,宽相同则高小的排在前面
  • 如果两两成一对,并且02的宽度相同、24的高度相同、0的高度和4的宽度相同则可以构成长方体
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

typedef struct Rect
{
	int width;
	int height;
}Rect;

bool operator<(const Rect &r1, const Rect &r2)
{
	if (r1.width < r2.width) return true;
	else if (r1.width == r2.width){
		if (r1.height < r2.height) return true;
		else return false;
	}
	else return false;
}

bool operator==(const Rect &r1, const Rect &r2)
{
	return r1.width == r2.width && r1.height == r2.height;
}

int main()
{
	vector<Rect> vecRec;
	string strLine;
	Rect rect;
	while (cin >> rect.width >> rect.height){
		if (rect.width > rect.height){
			rect.width ^= rect.height ^= rect.width ^= rect.height;
		}
		vecRec.push_back(rect);
		if (vecRec.size() == 6){
			sort(vecRec.begin(), vecRec.end());
			bool bPossible = false;
			if (vecRec[0] == vecRec[1] && vecRec[2] == vecRec[3] && vecRec[4] == vecRec[5]){
				if (vecRec[0].width == vecRec[2].width &&
					vecRec[0].height == vecRec[4].width &&
					vecRec[2].height == vecRec[4].height) bPossible = true;
			}
			if (bPossible) cout << "POSSIBLE" << endl;
			else cout << "IMPOSSIBLE" << endl;
			vecRec.clear();
		}
	}
	return 0;
}
/*
1345 2584
2584 683
2584 1345
683 1345
683 1345
2584 683
1234 4567
1234 4567
4567 4321
4322 4567
4321 1234
4321 1234
*/

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值