码迷,mamicode.com
首页 > 其他好文 > 详细

UVALive 5984

时间:2015-08-21 23:01:01      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:Save the Students!

技术分享
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<math.h>
using namespace std;

bool vis[205][205];   // 所有数据保证在1到50.那么坐标在-50到100之间。所以。哈希到50到200之间。开始开的200*200的。WA。

double dis(double x1, double y1, double x2, double y2)  // 两点间距离。
{
    // cout << x1 << "==" << y1 << "==" << x2<< "==" << y2 << "==" << endl;
    double xx = (x2-x1)*(x2-x1);
    double yy = (y2-y1)*(y2-y1);
    double ans = xx+yy+0.0;
    return ans;
}

int circle(int a, int b, int r)  //是否在圆内。
{
    int cnt = 0;
    for (int i=a-r; i<=a+r; ++i)
    {
        for (int j=b-r; j<=b+r; ++j)
        {
            if (!vis[i+100][j+100])
            {
                if (dis(i, j, a, b) <= r*r)
                {
                    cnt++;
                    vis[i+100][j+100] = 1;
                }
            }
        }
    }
    return cnt;
}

double area(int x1, int y1, int x2, int y2, int x3, int y3) // 三角形面积。
{
    double temp = (x2-x1)*(y3-y1) - (x3-x1)*(y2-y1);
    temp /= 2.0;
    if (temp < 0) temp = -temp;
    return temp;
}

int triangle(int x1, int y1, int x2, int y2, int x3, int y3) //是否在三角形内。
{
    int cnt = 0;
    int minx = min(x1, min(x2, x3));
    int maxx = max(x1, max(x2, x3));
    int miny = min(y1, min(y2, y3));
    int maxy = max(y1, max(y2, y3));

    for (int i=minx; i<=maxx; ++i)
    {
        for (int j=miny; j<=maxy; ++j)
        {
            if (!vis[i+100][j+100])
            {
                double s1 = area(i, j, x1, y1, x2, y2);
                double s2 = area(i, j, x1, y1, x3, y3);
                double s3 = area(i, j, x2, y2, x3, y3);
                double s = area(x1, y1, x2, y2, x3, y3);
                if (s1 + s2 + s3 == s)
                {
                    cnt++;
                    vis[i+100][j+100] = 1;
                }
            }
        }
    }
    return cnt;
}

int square(int x1, int y1, int l) //正方形直接判断是不是已经被覆盖了。就可以。开始用了一个巨蠢的用距离判断点是不是在正方形内。。呵呵。。。。
{
    int cnt = 0;
    for (int i=x1; i<=x1+l; ++i)
    {
        for (int j=y1; j<=y1+l; ++j)
        {
            if (!vis[i+100][j+100])
            {
                cnt++;
                vis[i+100][j+100] = 1;
            }
        }
    }
    return cnt;
}

int main()
{
    int t, n;
    int x1, x2, x3, x4, x5, x6;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        memset(vis, 0, sizeof(vis));
        int ans = 0;
        for (int i=0; i<n; ++i)
        {
            char temp;
            getchar();
            scanf("%c", &temp);
            if (temp == C)
            {
                scanf("%d%d%d", &x1, &x2, &x3);
                ans += circle(x1, x2, x3);
            }
            else if (temp == S)
            {
                scanf("%d%d%d", &x1, &x2, &x3);
                ans += square(x1, x2, x3);
            }
            else if (temp == T)
            {
                scanf("%d%d%d%d%d%d", &x1, &x2, &x3, &x4, &x5, &x6);
                ans += triangle(x1, x2, x3, x4, x5, x6);
            }
        }
        printf("%d\n", ans);
    }
    return 0;
}
View Code

 

UVALive 5984

标签:

原文地址:http://www.cnblogs.com/icode-girl/p/4749117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!