• ACM/ICPC 2011 AsiaAmritapuri 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;
    }
    举杯独醉,饮罢飞雪,茫然又一年岁。 ------AbandonZHANG
  • 相关阅读:
    js获取屏幕大小
    获取系统开机的时间(Windows、Linux)
    C++的STL中vector内存分配方法的简单探索
    服务器端如何判断客户端是不是手机
    测试简单for循环的效率
    多少钱都买不到这张表!百万都买不到这张表
    Unable to compile class for JSP
    windows上java中文乱码-指定字符集 -Dfile.encoding=UTF-8
    google翻译插件安装
    工作任务分配时的五个问题
  • 原文地址:https://www.cnblogs.com/AbandonZHANG/p/2598400.html
Copyright © 2020-2023  润新知