lightoj1211 Intersection of Cubes

思路:在一个三维坐标中,给出n个长方体,求所有长方体相交体积,每个长方体给定的是左下角和右上角的坐标。

所有相交体积必然属于两两相交的,而且随着长方体的增多,这个公共体积不会增加,所以呢,这个公共体积的左下角的xyz坐标必然是所有中最大的,而右上角的坐标必然是最小的。最后判断这个公共部分是否合理及是否可以组成一个长方体。

// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
// #define DEBUG
#ifdef DEBUG
#define debug(...) printf( __VA_ARGS__ )
#else
#define debug(...)
#endif
#define CLR(x) memset(x, 0,sizeof x)
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;}
template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;}
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct Point{
	double x, y;
	Point(){}
	Point(double _x, double _y){
		x = _x;
		y = _y;
	}
	Point operator + (const Point& rhs)const{
		return Point(x + rhs.x, y + rhs.y);
	}
	Point operator - (const Point& rhs)const{
		return Point(x - rhs.x, y - rhs.y);
	}
	double operator ^ (const Point& rhs)const{
		return (x * rhs.y - y * rhs.x);
	}
	double operator * (const Point& rhs)const{
		return (x * rhs.x + y * rhs.y);
	}
	Point operator * (const double Num)const{
		return Point(x * Num,y * Num);
	}
	friend ostream& operator << (ostream& output,const Point& rhs){
		output << "(" << rhs.x << "," << rhs.y << ")";
		return output;
	}
};
typedef Point Vector;
/*向量的模*/
inline double Length(const Vector& A){//Get the length of vector A;
	return sqrt(A * A);//sqrt(x*x+y*y);
}
/*向量夹角*/
inline double Angle(const Vector& A,const Point& B){
	return acos(A * B/Length(A)/Length(B));
}
/*向量A旋转rad弧度*/
inline Point Rotate(const Vector& A, double rad){
	return Vector(A.x*cos(rad) - A.y*sin(rad),A.x*sin(rad) + A.y*cos(rad));
}
int a[7];
int main()
{	// ios::sync_with_stdio(false);
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t, icase = 0;
	int n;
	cin >> t;
	while(t--){
		a[1] = a[2] = a[3] = 0;
		a[4] = a[5] = a[6] = inf;
		cin >> n;
		int tmp;
		while(n--){
			for (int i = 1;i <= 3;++i){
				cin >> tmp;
				if (tmp > a[i])
					a[i] = tmp;
			}
			for (int i = 4;i <= 6;++i){
				cin >> tmp;
				if (a[i] > tmp)
					a[i] = tmp;
			}
		}
		if (a[4] > a[1] && a[5] > a[2] && a[6] > a[3]){
			printf("Case %d: %d\n", ++icase, (a[4] - a[1])*(a[5] - a[2])*(a[6] - a[3])); 
		}
		else printf("Case %d: 0\n", ++icase);
	}
	return 0;
}


你可能感兴趣的:(lightoj,基础几何)