题目链接:https://www.spoj.com/problems/AMR11B/en/
VJ暑期训练链接:https://vjudge.net/contest/237052#problem/B
AMR11B - Save the Students
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
#include<stdio.h> #include<string.h> #include<math.h> using namespace std; #define max(x,y) x>y?x:y #define min(x,y) x<y?x:y int flag[500][500]; int sx[3]; int sy[3]; int sslo(int x0,int y0,int x1,int y1,int x2,int y2) { return abs(x0*(y1-y2)+x1*(y2-y0)+x2*(y0-y1));//已知3点计算三角形面积 } bool sanjiaoxing(int x,int y) { int S=sslo(sx[0],sy[0],sx[1],sy[1],sx[2],sy[2]); int S1=sslo(x,y,sx[1],sy[1],sx[2],sy[2])+sslo(sx[0],sy[0],x,y,sx[2],sy[2])+sslo(sx[0],sy[0],sx[1],sy[1],x,y); if(S==S1) return true;//面积法 return false; } int main() { int T; scanf("%d",&T); while (T--) { memset(flag,0,sizeof(flag)); int sum=0; int N; scanf("%d",&N); while (N--) { getchar(); char a; scanf("%c",&a); if(a=='T')//三角形 { int maxx=0,minx=999999999,maxy=0,miny=999999999; for (int i=0;i<3;i++) { scanf("%d%d",&sx[i],&sy[i]); sx[i]=sx[i]+100; sy[i]=sy[i]+100; maxx=max(sx[i],maxx); minx=min(sx[i],minx); maxy=max(sy[i],maxy); miny=min(sy[i],miny); } for (int j=miny;j<=maxy;j++) for (int i=minx;i<=maxx;i++) { if(sanjiaoxing(i,j)==true&&flag[i][j]==0) { sum++; flag[i][j]=1; } } } else if(a=='C')//圆形 { int x,y,r; scanf("%d%d%d",&x,&y,&r); x+=100;y+=100; for (int j=y-r;j<=y+r;j++) for (int i=x-r;i<=x+r;i++) { if((i-x)*(i-x)+(j-y)*(j-y)<=r*r) { if(flag[i][j]==0) { sum++; flag[i][j]=1; } } } } else if(a=='S')//正方形 { int x,y,l; scanf("%d%d%d",&x,&y,&l); x+=100;y+=100; for (int j=y;j<=y+l;j++) for (int i=x;i<=x+l;i++) { if(flag[i][j]==0) { sum++; flag[i][j]=1; } } } } printf("%d ",sum); } return 0; }