ACM/ICPC 2011 Asia-Amritapuri Site / B Save the Students!(判断点在三角形中)

Problem B: 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.
Time Limit: 3 s
Memory Limit: 32 MBytes

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

Notes/Explanation of Sample Input:

Illustration of Testcase 4.

Testcase 4

 

分析:

题目范围不大,暴力枚举即可,就是注意判断点在圆、正方形、三角形中怎么判断。

代码:

 

#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;


struct   CAPoint
{
    int   x;
    int   y;
};


//返回向量叉乘,公共点为Po3
int   VecMultiply(CAPoint   po1,   CAPoint   po2,   CAPoint   po3)
{
        return   ( (po1.x-po3.x)*(po2.y-po3.y)-(po2.x-po3.x)*(po1.y-po3.y) );
}


int min(int a,int b)
{
    return a<b?a:b;
}


int max(int a,int b)
{
    return a>b?a:b;
}

//判断点p是否是三角形内   p1,p2,p3为三角形的三个顶点 (包含顶点和边)
bool   PoInTrigon(CAPoint   p1,   CAPoint   p2,   CAPoint   p3,   CAPoint   p)
{
    int   re1   =   VecMultiply(p1,   p   ,   p2);
    int   re2   =   VecMultiply(p2,   p   ,   p3);
    int   re3   =   VecMultiply(p3,   p   ,   p1);
    if   ((re1   ==  0) &&  (p.x>=min(p1.x,p2.x))    &&  (p.x<=max(p1.x,p2.x))  &&  (p.y>=min(p1.y,p2.y))  &&  (p.y<=max(p1.y,p2.y)))
            return true;
    if   ((re2   ==  0) &&  (p.x>=min(p3.x,p2.x))    &&  (p.x<=max(p3.x,p2.x))  &&  (p.y>=min(p3.y,p2.y))  &&  (p.y<=max(p3.y,p2.y)))
            return true;
    if   ((re3   ==  0) &&  (p.x>=min(p1.x,p3.x))    &&  (p.x<=max(p1.x,p3.x))  &&  (p.y>=min(p1.y,p3.y))  &&  (p.y<=max(p1.y,p3.y)))
            return true;
    if   ((re1   > 0   &&   re2   >   0   &&   re3   >   0   )   ||
        (re1   <0   &&   re2   <   0   &&   re3   <   0   ))
            return   true;


    return   false;
}


int main()
{
    int t;
    cin>>t;
    while (t--)
    {
        int n;
        cin>>n;
        int f[500][500];
        memset(f,0,sizeof(f));
        for (int i=0;i<n;i++)
        {
            char c;
            cin>>c;
            if (c=='C')
            {
                int x,y,r;
                cin>>x>>y>>r;
                for (int i=-110;i<=110;i++)
                    for (int j=-110;j<=110;j++)
                    {
                        if (sqrt((i-x)*(i-x)+(j-y)*(j-y))<=r)
                            f[i+110][j+110]=1;
                    }
            }
            else if (c=='S')
            {
                int x,y,l;
                cin>>x>>y>>l;
                for (int i=x;i<=x+l;i++)
                    for (int j=y;j<=y+l;j++)
                        f[i+110][j+110]=1;
            }
            else
            {
                CAPoint p1,p2,p3,p;
                cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y;
                for (int i=-110;i<=110;i++)
                    for (int j=-110;j<=110;j++)
                        {
                            p.x=i;
                            p.y=j;
                            if (PoInTrigon(p1,p2,p3,p))
                                f[p.x+110][p.y+110]=1;
                        }
            }
        }
        int num=0;
        for (int i=-110;i<=110;i++)
            for (int j=-110;j<=110;j++)
                if (f[i+110][j+110]==1)
                    num++;
        cout<<num<<endl;
    }
    return 0;
}

转载于:https://www.cnblogs.com/AbandonZHANG/archive/2012/07/18/2598400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值