陈老师的多校联合20140816||SPOJ AMR11B 三角形的有向面积

来源:互联网 发布:杭州做网络推广的公司 编辑:程序博客网 时间:2024/05/21 10:49

http://www.spoj.com/problems/AMR11B/

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

题目大意:给定你个区域可以为三角形,圆,正方形,判断有多少点在这些区域的内部或者是边界上。

解题思路:本来是个水题,但是我通过他发现了一个以前知识的错误。三角形的有向面积,入门经典上讲错了!

ac代码如下:

#include <stdio.h>#include <string.h>#include <iostream>using namespace std;int a[400][400];int judge(int x0,int y0,int x1,int y1,int x2,int y2){    return x0*y1+x2*y0+x1*y2-x2*y1-x0*y2-x1*y0;}int abs(int x){    if(x<0)        return -x;    return x;}void triangle(int x0,int y0,int x1,int y1,int x2,int y2){    for(int i=0;i<=60;i++)        for(int j=0;j<=60;j++)        {            int s=judge(x0,y0,x1,y1,x2,y2);            int s1=judge(i,j,x0,y0,x1,y1);            int s2=judge(i,j,x1,y1,x2,y2);            int s3=judge(i,j,x2,y2,x0,y0);            if(s>=0&&s1>=0&&s2>=0&&s3>=0)                a[i+200][j+200]=1;            else if(s<=0&&s1<=0&&s2<=0&&s3<=0)                a[i+200][j+200]=1;        }}void circle(int x,int y,int r){    for(int i=-55;i<=105;i++)        for(int j=-55;j<=105;j++)        {            if((abs(i-x)*abs(i-x)+abs(j-y)*abs(j-y)<=r*r))                 a[i+200][j+200]=1;        }}void square(int x,int y,int l){    int num=0;    for(int i=x;i<=x+l;i++)        for(int j=y;j<=l+y;j++)        {              a[i+200][j+200]=1;              num++;        }}int main(){    int T;    scanf("%d",&T);    while(T--)    {        int n;        memset(a,0,sizeof(a));        scanf("%d",&n);        for(int i=0;i<n;i++)        {            char t;            scanf("\n%c",&t);            if(t=='C')            {                int x,y,r;                scanf("%d%d%d",&x,&y,&r);                circle(x,y,r);            }            else if(t=='T')            {                int x0,x1,x2,y0,y1,y2;                scanf("%d%d%d%d%d%d",&x0,&y0,&x1,&y1,&x2,&y2);                triangle(x0,y0,x1,y1,x2,y2);            }            else if(t=='S')            {                int x,y,l;                scanf("%d%d%d",&x,&y,&l);                square(x,y,l);            }        }        int cnt=0;        for(int i=0;i<350;i++)            for(int j=0;j<350;j++)                 if(a[i][j]==1)                      cnt++;        printf("%d\n",cnt);    }    return 0;}/**41C 5 5 21S 3 3 41T 1 1 1 3 3 13C 10 10 3S 9 8 4T 7 9 10 8 8 10**/


0 0
原创粉丝点击