SPOJ AMR11B:Save the Students_判断点是否在圆、矩形、三角形内

来源:互联网 发布:奇偶算法 编辑:程序博客网 时间:2024/06/08 07:57

Hogwarts is under attack by the Dark Lord, He-Who-Must-Not-Be-Named. To protect the students, Harry Potter must cast protective spells so that those who are protected by the spells cannot be attacked by the Dark Lord.

Harry has asked all the students to gather on the vast quidditch sports field so that he can cast his spells.  The students are standing in a 2D plane at all grid points - these are the points (x,y) such that both x and y are integers (positive, negative or 0). Harry's spell can take the shapes of triangle, circle or square, and all who fall within that shape (including its boundaries) are protected.

Given the types of spells and the details regarding where Harry casts the spell, output the number of people saved by Harry's spells.

 

Input (STDIN):

The first line contains the number of test cases T. T test cases follow.

Each case contains an integer N on the first line, denoting the number of spells Harry casts. N lines follow, each containing the description of a spell.

If the ith spell is a triangle, then the line will be of the form "T x1 y1 x2 y2 x3 y3". Here, (x1,y1), (x2,y2) and (x3,y3) are the coordinates of the vertices of the triangle.

If the ith spell is a circle, then the line will be of the form "C x y r". Here, (x,y) is the center and r is the radius of the circle.

If the ith spell is a square, then the line will be of the form "S x y l". Here, (x,y) denotes the coordinates of the bottom-left corner of the square (the corner having the lowest x and y values) and l is the length of each side.

 

Output (STDOUT):

Output T lines, one for each test case, denoting the number of people Harry can save.

 

Constraints:

All numbers in the input are integers between 1 and 50, inclusive.

The areas of all geometric figures will be > 0.

 

Sample Input:

4

1

C 5 5 2

1

S 3 3 4

1

T 1 1 1 3 3 1 

3

C 10 10 3

S 9 8 4

T 7 9 10 8 8 10

 

Sample Output:

13

25

6

34

 

//题意:给出在2D坐标上给出n个几何图形(squar,circle,triangle),问最多能够覆盖多少个点。

//分析:由于数据量不大,所以直接利用标记法。针对每个几何图形,将该图的最大外接四边形内的所有点与之分析看是否被覆盖!

#include<stdio.h>#include<string.h>#include<math.h>using namespace std;#define maxn 305 #define eps 1e-7#define max(a,b) (a>b?a:b)#define min(a,b) (a<b?a:b)struct point_float{float x,y;};bool used[maxn][maxn];int ans;int Len(int x1,int y1,int x2,int y2){return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);}void make_C(int x,int y,int r)  //点是否在圆内{int i,j;for(i=x-r;i<=x+r;i++)for(j=y-r;j<=y+r;j++)if(!used[i][j])if(Len(i,j,x,y)<=r*r){used[i][j]=true;ans++;}}void make_S(int x,int y,int l)  //点是否在正方形内{int i,j;for(i=x;i<=x+l;i++)for(j=y;j<=y+l;j++)if(!used[i][j]){used[i][j]=true;ans++;}}float GetTriangleSquar(const point_float pt0,const point_float pt1,const point_float pt2)  //求三角形面积{point_float AB,BC;AB.x=pt1.x-pt0.x;AB.y=pt1.y-pt0.y;BC.x=pt2.x-pt1.x;BC.y=pt2.y-pt1.y;return fabs((AB.x*BC.y-AB.y*BC.x))/2.0f;}void make_T(const point_float A,const point_float B,const point_float C) //点是否在三角形内,面积法{float SABC,SADB,SBDC,SADC;int i,j;point_float D;//以三角形最大外围遍历,直接遍历本题最大数据范围要超时 int minx=min(min((int)A.x,(int)B.x),(int)C.x);int miny=min(min((int)A.y,(int)B.y),(int)C.y);int maxx=max(max((int)A.x,(int)B.x),(int)C.x);int maxy=max(max((int)A.y,(int)B.y),(int)C.y);for(i=minx;i<=maxx;i++)for(j=miny;j<=maxy;j++)if(!used[i][j]){D.x=i*1.0,D.y=j*1.0;SABC=GetTriangleSquar(A,B,C);SADB=GetTriangleSquar(A,D,B);SBDC=GetTriangleSquar(B,D,C);SADC=GetTriangleSquar(A,D,C);float SumSquar=SADB+SBDC+SADC;if(fabs(SABC-SumSquar)<eps){used[i][j]=true;ans++;}}}int main(){int t,n,x,y,r,l;char s[2];scanf("%d",&t);while(t--){scanf("%d",&n);ans=0;memset(used,false,sizeof(used));while(n--){scanf("%s",s);if(s[0]=='C'){scanf("%d%d%d",&x,&y,&r);x+=100,y+=100;  //数据输入为1-50,但是圆可能覆盖到为负值的点make_C(x,y,r);}else if(s[0]=='S'){scanf("%d%d%d",&x,&y,&l);x+=100,y+=100;make_S(x,y,l);}else if(s[0]=='T'){point_float A,B,C;scanf("%f%f%f%f%f%f",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y);A.x+=100.0,A.y+=100.0; B.x+=100.0,B.y+=100.0;C.x+=100.0,C.y+=100.0;make_T(A,B,C);}}printf("%d\n",ans);}return 0;}


原创粉丝点击