UVALive 5984 Save the Students!(幾何)

題目鏈接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26402


題意:給n個圖形,有三角形,圓形,正方形,求覆蓋了多少個整點。

題解:暴力枚舉點,判斷是否在所給的圖形內。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#define ll long long

using namespace std;

const int N=180;

struct Point {
    int x,y,r;
};

///判斷是否在正方形內
bool is_S(Point a,Point s) {
    return (a.x>=s.x)&&(a.x<=s.x+s.r)&&(a.y>=s.y)&&(a.y<=s.y+s.r);
}

///判斷是否在園內
bool is_C(Point a,Point c) {
    return (a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y)<=c.r*c.r;
}

///叉積
int xm(Point p1,Point p2,Point p0) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

///判斷是否在三角型內
int ins(Point q,int n,Point p[]) {
    int s[3]= {1,1,1};
    for(int i=0; i<n&&s[1]|s[2]; i++) {
        int x=xm(p[(i+1)%n],q,p[i]);
        if(x>0)s[1]=0;
        else if(x<0)s[2]=0;
        else s[0]=0;
    }
    return s[1]|s[2];
}

Point C[N],S[N],T[N];
int lc,ls,lt;
int n;

int main() {
    // freopen("test.in","r",stdin);
    int t;
    cin>>t;
    while(t--) {
        scanf("%d",&n);
        char c;
        lc=0,ls=0,lt=0;
        for(int i=0; i<n; i++) {
            scanf(" %c",&c);
            if(c=='C') {
                scanf("%d%d%d",&C[lc].x,&C[lc].y,&C[lc].r);
                lc++;
            } else if(c=='S') {
                scanf("%d%d%d",&S[ls].x,&S[ls].y,&S[ls].r);
                ls++;
            } else {
                scanf("%d%d",&T[lt].x,&T[lt].y);
                lt++;
                scanf("%d%d",&T[lt].x,&T[lt].y);
                lt++;
                scanf("%d%d",&T[lt].x,&T[lt].y);
                lt++;
            }
        }
        int ans=0;
        for(int i=-60; i<=110; i++) {
            for(int j=-60; j<=110; j++) {
                bool f=0;
                Point io;
                io.x=i,io.y=j;
                for(int k=0; k<lc; k++) {
                    if(is_C(io,C[k])) {
                        ans++,f=1;
                        break;
                    }
                }
                if(f)continue;
                for(int k=0; k<ls; k++) {
                    if(is_S(io,S[k])) {
                        ans++,f=1;
                        break;
                    }
                }
                if(f) continue;
                Point p[3];
                for(int k=0; k+2<lt; k+=3) {
                    p[0]=T[k],p[1]=T[k+1],p[2]=T[k+2];
                    if(ins(io,3,p)) {
                        ans++;
                        break;
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章