您的位置:首页 > 其它

HDU 2892 area

2015-08-16 12:01 375 查看
链接:http://acm.hdu.edu.cn/showproblem.php?pid=2892

area

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 740 Accepted Submission(s): 287

Problem Description

小白最近被空军特招为飞行员,参与一项实战演习。演习的内容是轰炸某个岛屿。。。

作为一名优秀的飞行员,任务是必须要完成的,当然,凭借小白出色的操作,顺利地将***投到了岛上某个位置,可是长官更关心的是,小白投掷的***到底摧毁了岛上多大的区域?

岛是一个不规则的多边形,而***的爆炸半径为R。

小白只知道自己在(x,y,h)的空间坐标处以(x1,y1,0)的速度水平飞行时投下的***,请你计算出小白所摧毁的岛屿的面积有多大. 重力加速度G = 10.

Input

首先输入三个数代表小白投弹的坐标(x,y,h);

然后输入两个数代表飞机当前的速度(x1, y1);

接着输入***的爆炸半径R;

再输入一个数n,代表岛屿由n个点组成;

最后输入n行,每行输入一个(x',y')坐标,代表岛屿的顶点(按顺势针或者逆时针给出)。(3<= n < 100000)

Output

输出一个两位小数,表示实际轰炸到的岛屿的面积。

Sample Input

0 0 2000

100 0

100

4

1900 100

2000 100

2000 -100

1900 -100

Sample Output

15707.96

Source

2009 Multi-University Training Contest 10 - Host by NIT

Recommend

gaojie

大意——给你一个起点空间坐标,再给你一个平面坐标,空间坐标表示此刻导弹的位置,平面坐标表示导弹移动的方向。导弹做的是平抛运动。现在再给你导弹的轰炸半径以及一个多边形小岛的顶点坐标。问:导弹能轰炸岛屿的多大面积?

思路——实际上就是求圆与多边形相交的面积。如果是凸多边形,可以将多边形化作以圆心为顶点的三角形;但如果是凹多边形,那么会因为求出多余面积而难以处理。但是用叉积算可以避免这个问题。因此找到一个模板套用就行了。

复杂度分析——时间复杂度:O(n),空间复杂度:O(n)

附上AC代码:

// 程序中的模板是我修改的,除了主函数,其他函数都属于模板部分

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <map>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double pi = acos(-1.0);
const double e = exp(1.0);
const double eps = 1e-8;
const int maxn = 100005;
double x, y, h;
double vx, vy;
double R;
int n;
struct point
{
	double x, y;
	point(double _x=0.0, double _y=0.0)
		: x(_x), y(_y) {}
	point operator - (const point & p)
	{
		return point(x-p.x, y-p.y);
	}
	double sqrx()
	{
		return sqrt(x*x+y*y);
	}
} area[maxn];

double xmult(point & p1, point & p2, point & p0);
double distancex(point & p1, point & p2);
point intersection(point u1, point u2, point v1, point v2);
void intersection_line_circle(point c, double r, point l1, point l2, point & p1, point & p2);
point ptoseg(point p, point l1, point l2);
double distp(point & a, point & b);
double Direct_Triangle_Circle_Area(point a, point b, point o, double r);

int main()
{
	ios::sync_with_stdio(false);
	while (scanf("%lf%lf%lf", &x, &y, &h) != EOF)
	{
		scanf("%lf%lf", &vx, &vy);
		scanf("%lf", &R);
		scanf("%d", &n);
		x += vx*sqrt(h/5.0);
		y += vy*sqrt(h/5.0);
		point temp = point(x, y);
		double sum = 0;
		for (int i=0; i<n; i++)
			scanf("%lf%lf", &area[i].x, &area[i].y);
		for (int i=0; i<n-1; i++)
			sum += Direct_Triangle_Circle_Area(area[i], area[i+1], temp, R);
		sum += Direct_Triangle_Circle_Area(area[n-1], area[0], temp, R);
		printf("%.2f\n", fabs(sum));
	}
	return 0;
}

double xmult(point & p1, point & p2, point & p0)
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}

double distancex(point & p1, point & p2)
{
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

point intersection(point u1, point u2, point v1, point v2)
{
	point ret = u1;
	double t = ((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
			 / ((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
	ret.x += (u2.x-u1.x)*t;
	ret.y += (u2.y-u1.y)*t;
	return ret;
}

void intersection_line_circle(point c, double r, point l1, point l2, point & p1, point & p2)
{
	point p = c;
	double t;
	p.x += l1.y-l2.y;
	p.y += l2.x-l1.x;
	p = intersection(p, c, l1, l2);
	t = sqrt(r*r-distancex(p, c)*distancex(p, c))/distancex(l1, l2);
	p1.x = p.x+(l2.x-l1.x)*t;
	p1.y = p.y+(l2.y-l1.y)*t;
	p2.x = p.x-(l2.x-l1.x)*t;
	p2.y = p.y-(l2.y-l1.y)*t;
}

point ptoseg(point p, point l1, point l2)
{
	point t = p;
	t.x += l1.y-l2.y;
	t.y += l2.x-l1.x;
	if (xmult(l1, t, p)*xmult(l2, t, p)>eps)
		return distancex(p, l1)<distancex(p, l2) ? l1 : l2;
	return intersection(p, t, l1, l2);
}

double distp(point & a, point & b)
{
	return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}

double Direct_Triangle_Circle_Area(point a, point b, point o, double r)
{
	double sign = 1.0;
	a = a-o;
	b = b-o;
	o = point(0.0, 0.0);
	if (fabs(xmult(a, b, o)) < eps)
		return 0.0;
	if (distp(a, o) > distp(b, o))
	{
		swap(a, b);
		sign = -1.0;
	}
	if (distp(a, o) < r*r+eps)
	{
		if (distp(b, o) < r*r+eps)
			return xmult(a, b, o)/2.0*sign;
		point p1, p2;
		intersection_line_circle(o, r, a, b, p1, p2);
		if (distancex(p1, b) > distancex(p2, b))
			swap(p1, p2);
		double ret1 = fabs(xmult(a, p1, o));
		double ret2 = acos((p1.x*b.x+p1.y*b.y)/p1.sqrx()/b.sqrx())*r*r;
		double ret = (ret1+ret2)/2.0;
		if (xmult(a, b, o)<eps && sign>0.0 || xmult(a, b, o)>eps && sign<0.0)
			ret = -ret;
		return ret;
	}
	point ins = ptoseg(o, a, b);
	if (distp(o, ins)>r*r-eps)
	{
		double ret = acos((a.x*b.x+a.y*b.y)/a.sqrx()/b.sqrx())*r*r/2.0;
		if (xmult(a, b, o)<eps && sign>0.0 || xmult(a, b, o)>eps && sign<0.0)
			ret = -ret;
		return ret;
	}
	point p1, p2;
	intersection_line_circle(o, r, a, b, p1, p2);
	double cm = r/(distancex(o, a)-r);
	point m = point((o.x+cm*a.x)/(1+cm), (o.y+cm*a.y)/(1+cm));
	double cn = r/(distancex(o, b)-r);
	point n = point((o.x+cn*b.x)/(1+cn), (o.y+cn*b.y)/(1+cn));
	double ret1 = acos((m.x*n.x+m.y*n.y)/m.sqrx()/n.sqrx())*r*r;
	double ret2 = acos((p1.x*p2.x+p1.y*p2.y)/p1.sqrx()/p2.sqrx())*r*r-fabs(xmult(p1, p2, o));
	double ret = (ret1-ret2)/2.0;
	if (xmult(a, b, o)<eps && sign>0.0 || xmult(a, b, o)>eps && sign<0.0)
		ret = -ret;
	return ret;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: