223. Rectangle Area

class matrix{
private:
	static const int TOTOP = 0x8;
	static const int TOBOTTOM = 0x4;
	static const int TOLEFT = 0x2;
	static const int TORIGHT = 1;
	static const int TOMARK = 0x0f;
	static const int INMARK = 0xf0;
	static const int INTOP = 0x80;
	static const int INBOTTOM = 0x40;
	static const int INLEFT = 0x20;
	static const int INRIGHT = 0x10;
	static const int GRAPEWID = 0x100;
	static const int GRAPELEN = 0x200;
	int leftX;
	int leftY;
	int rightX;
	int rightY;
public:
	matrix(int lx, int ly, int rx, int ry) : leftX(lx), leftY(ly), rightX(rx), rightY(ry) {};
	
	int toAnother(const matrix &m){
		int ans = 0;
		if(leftY >= m.rightY)
			ans |= TOTOP;
		if(rightY <= m.leftY)
			ans |= TOBOTTOM;
		if(rightX <= m.leftX)
			ans |= TOLEFT;
		if(leftX >= m.rightX)
			ans |= TORIGHT;
		if(rightY >= m.rightY && leftY <= m.leftY){
			ans |= GRAPEWID;
		}else{
			
			if(leftY < m.rightY && leftY > m.leftY)
				ans |= INTOP;
			if(rightY < m.rightY && rightY > m.leftY)
				ans |= INBOTTOM;
		}
		if(rightX >= m.rightX && leftX <= m.leftX){
			ans |= GRAPELEN;
		}else{	
			if(rightX < m.rightX && rightX > m.leftX)
				ans |= INLEFT;
			if(leftX < m.rightX && leftX > m.leftX)
				ans |= INRIGHT;
		}
		return ans;
	}

	int commonArea(const matrix &m){
		int loc = toAnother(m);
		if(loc & TOMARK)
			return 0;
		int len = 0, wid = 0;
		if(loc & GRAPEWID)
			wid = m.rightY - m.leftY;
		else if( loc & INTOP && loc & INBOTTOM)
			wid = rightY - leftY;
		else if(loc & INTOP)
			wid = m.rightY - leftY;
		else
			wid = rightY - m.leftY;
		if(loc & GRAPELEN)
			len = m.rightX - m.leftX;
		else if(loc & INLEFT && loc & INRIGHT)
			len = rightX - leftX;
		else if(loc & INLEFT)	
			len = rightX - m.leftX;
		else
			len = m.rightX - leftX;
		return len * wid;	
	}

	int Area(){
		return (rightX - leftX) * (rightY - leftY);
	}
};

class Solution {
public:
    int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
		matrix m1(A,B,C,D);
		matrix m2(E,F,G,H);

		return m1.Area() + m2.Area() - m1.commonArea(m2);
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值