hdoj.4404 Worms【计算几何+圆与多边形相交面积】 2015/08/17
2015-08-17 19:48
465 查看
Worms
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 484 Accepted Submission(s): 254
[align=left]Problem Description[/align]
Worms is a series of turn-based computer games. Players control a small platoon of earthworms across a deformable landscape, battling other computer- or player-controlled teams. The game feature bright and
humorous cartoon-style animation and a varied arsenal of bizarre weapons.
![](http://acm.hdu.edu.cn/data/images/C417-1005-1.jpg)
During the course of the game, players take turns selecting one of their worms. They then use whatever tools and weapons available to attack and kill the opponents’ worms. Over fifty weapons and tools may be available each time a game is played, and differing
selections of weapons and tools can be saved into a “scheme” for easy selection in future games.
When most weapons are used, they cause explosions that deform the terrain, creating circular cavities. If a worm is hit by a weapon, the amount of damage dealt to the worm will be removed from the worm’s initial amount of health. When a worm fall into the water
or its health is reduced to zero, it dies.
In this problem, the terrain of a stone can be described as a simple polygon. The worms only use the time bombs. Once a time bomb is thrown, it is only attracted by the force of gravity. In other words, the flying track of the bomb is a parabola. When it reaches
the stone (the polygon), the bomb does not blow off or stop immediately. It will still fly along the parabola regardless of the resistance of the stone due to its special character. The time bomb can only be triggered by the timer. When the preset time is
used up, the bomb blows up and eliminates all the materials within its explosion range. You need to calculate the area of the eliminated materials of one explosion.
[align=left]Input[/align]
There are multiple test cases.
The first line of a test case contains seven floating numbers x0, y0, v0, θ, t, g, R. (x0, y0) is the position of the worm who throws the bomb, which could be inside the polygon or outside the polygon (even in the sky or under the water). The initial value
of velocity is v0 which forms aθ (0≤θ<90) angle with the positive direction of x-axis. The bomb is always thrown upwards. The preset time is t, which is also the time of flying. The value of acceleration of gravity of the worms’ planet is g. The direction
of gravity is the negative direction of y-axis. The explosion range is a circle and R is the radius.
The second line contains an integer n (3≤n≤100), which indicates the number of edges of the stone(simple polygon). The following n lines contain two real numbers xi and yi each, which describe the coordinates of a vertex. Two vertexes in adjacent lines are
adjacent on the polygon.
The input contains multiple test cases. It is ended by “0 0 0 0 0 0 0”.
[align=left]Output[/align]
For each test case, output one line containing the area of the eliminated materials of the explosion rounded to two digits to the right of the decimal point.
[align=left]Sample Input[/align]
0 0 15 45 10 2 10 3 100 0 200 0 100 100 0 0 0 0 0 0 0
[align=left]Sample Output[/align]
228.74
[align=left]Source[/align]
2012 ACM/ICPC Asia Regional Jinhua Online
注:圆与多边形相交面积模板,根据输入的x0, y0, v0, θ, t, g,可以求出t 时间时刻bomb的坐标,即圆心坐标
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> using namespace std; const double eps = 1e-8; const double PI = acos(-1.0); int dcmp(double x){ if( x > eps ) return 1; return x < -eps ? -1 : 0; } struct Point{ double x,y; Point(){ x = y = 0; } Point(double a,double b){ x = a;y = b; } inline void input(){ scanf("%lf%lf",&x,&y); } inline Point operator-(const Point &b)const{ return Point(x - b.x,y - b.y); } inline Point operator+(const Point &b)const{ return Point(x + b.x,y + b.y); } inline Point operator*(const double &b)const{ return Point(x * b,y * b); } inline double dot(const Point &b)const{ return x * b.x + y * b.y; } inline double cross(const Point &b,const Point &c)const{ return (b.x - x) * (c.y - y) - (c.x - x) * (b.y - y); } inline double Dis(const Point &b)const{ return sqrt((*this-b).dot(*this-b)); } inline bool InLine(const Point &b,const Point &c)const{ //三点共线 return !dcmp(cross(b,c)); } inline bool OnSeg(const Point &b,const Point &c)const{ //点在线段上,包括端点 return InLine(b,c) && (*this - c).dot(*this - b) < eps; } }; inline double min(double a,double b){ return a < b ? a : b; } inline double max(double a,double b){ return a > b ? a : b; } inline double Sqr(double x){ return x * x; } inline double Sqr(const Point &p){ return p.dot(p); } Point LineCross(const Point &a,const Point &b,const Point &c,const Point &d){ double u = a.cross(b,c) , v = b.cross(a,d); return Point((c.x * v + d.x * u) / (u + v) , (c.y * v + d.y * u) / (u + v)); } double LineCrossCircle(const Point &a,const Point &b,const Point &r, double R,Point &p1,Point & p2){ Point fp = LineCross(r , Point(r.x+a.y-b.y , r.y+b.x-a.x) , a , b); double rtol = r.Dis(fp); double rtos = fp.OnSeg(a , b) ? rtol : min(r.Dis(a) , r.Dis(b)); double atob = a.Dis(b); double fptoe = sqrt(R * R - rtol * rtol) / atob; if( rtos > R - eps ) return rtos; p1 = fp + (a - b) * fptoe; p2 = fp + (b - a) * fptoe; return rtos; } double SectorArea(const Point &r,const Point &a,const Point &b,double R){ //不大于180度扇形面积,r->a->b逆时针 double A2 = Sqr(r - a) , B2 = Sqr(r - b) , C2 = Sqr(a - b); return R * R * acos( (A2 + B2 - C2) * 0.5 / sqrt(A2) / sqrt(B2)) * 0.5; } double TACIA(const Point &r,const Point &a,const Point &b,double R){ double adis = r.Dis(a) , bdis = r.Dis(b); if( adis < R + eps && bdis < R + eps ) return r.cross(a , b) * 0.5; Point ta , tb; if( r.InLine(a,b) ) return 0.0; double rtos = LineCrossCircle(a, b, r, R, ta, tb); if( rtos > R - eps ) return SectorArea(r, a, b, R); if( adis < R + eps ) return r.cross(a, tb) * 0.5 + SectorArea(r, tb, b, R); if( bdis < R + eps ) return r.cross(ta, b) * 0.5 + SectorArea(r, a, ta, R); return r.cross(ta, tb) * 0.5 + SectorArea(r, tb, b, R) + SectorArea(r, a, ta, R); } const int MAXN = 105; Point p[MAXN]; double SPICA(int n,Point r,double R){ int i; double ret = 0 , if_clock_t; for( i = 0 ; i < n ; ++i ){ if_clock_t = dcmp(r.cross(p[i], p[(i + 1) % n])); if( if_clock_t < 0 ) ret -= TACIA(r, p[(i + 1) % n], p[i], R); else ret += TACIA(r, p[i], p[(i + 1) % n], R); } return fabs(ret); } int main(){ double xa,ya,v,o,t,g,R; int n,i; while( ~scanf("%lf%lf%lf%lf%lf%lf%lf",&xa,&ya,&v,&o,&t,&g,&R) ){ if( !xa&&!ya&&!v&&!o&&!t&&!g&&!R ) break; Point circle = Point( (xa + v * cos(o*PI/180.0) * t) , (ya + v * sin(o*PI/180.0) * t - 0.5 * g * t * t) ); //printf("____%.2lf %.2lf\n",circle.x,circle.y); scanf("%d",&n); for( i = 0 ; i < n ; ++i ) p[i].input(); printf("%.2lf\n",SPICA(n,circle,R)); } return 0; }
相关文章推荐
- 常用排序算法稳定性、时间复杂度分析(转,有改动)
- 多线程知识(1)
- 算法竞赛入门经典:第六章 数据结构基础 6.1卡片游戏
- extjs_04_grid(弹出窗口&行编辑器 CRUD数据)
- 杭电1596find the safest road
- C++11新特性之自动类型判断以及匿名函数
- 异常“DMP”引起的误会
- linux找不到动态链接库 .so文件的解决方法
- charles用法详解
- WebRTC入门篇
- 浮点与定点的转换
- LightOJ 1422 Halloween Costumes(区间dp)
- 何时使用条件变量? 我们有互斥锁不是就够了么?
- Spring---AOP原理浅析
- 算法竞赛入门经典:第五章 基础题目选解 5.12多少块土地
- some tools
- C++11新特性之std::bind与std::function
- 20150814 JavaScript深入浅出下
- 杭电2066一个人的旅行
- Linux 指定运行时动态库路径