TOJ 3853 Farmer Flabby

Code pattern by kuangbin;

The portal:http://acm.tju.edu.cn/toj/showp3853.html

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <iostream>

using namespace std;

const double eps =1e-8;
const double PI=acos(-1.0);

struct Point {
    double x,y;
    Point(){}
    Point(double _x,double _y) {
        x=_x;
        y=_y;
    }
    Point operator -(const Point &b) const {
        return Point(x-b.x,y-b.y);
    }
    
    double operator ^(const Point &b) const {
        return x*b.y - y*b.x;
    }
    
    double operator *(const Point &b) const {
        return x*b.x + y*b.y;
    }
};

struct Line {
    Point s,e;
    Line(){}
    Line(Point _s,Point _e) {
        s=_s;
        e=_e;
    }
};

int sgn(double x) {
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}


bool Onseg(Point P,Line L) {
    return
    sgn((L.s-P)^(L.e-P)) ==0 &&
    sgn((P.x-L.s.x) * (P.x - L.e.x)) <= 0 &&
    sgn((P.y-L.s.y) * (P.y - L.e.y)) <=0;
}


int inConvexPoly(Point a,Point p[],int n) {
    for(int i=0; i<n; i++) {
        if(sgn((p[i]-a)^(p[(i+1)%n]-a)) <0) return -1;
        else if(Onseg(a,Line(p[i],p[(i+1)%n]))) return 0;
    }
    return 1;
}


void Deal_with() {
    int T,n,m;
    Point sheep[100005];
    Point bar[55];
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        for(int i = 0 ; i < n ; i++) {
            scanf("%lf %lf",&sheep[i].x,&sheep[i].y);
        }
        scanf("%d",&m);
        for(int i = 0 ; i < m ; i++) {
            scanf("%lf %lf",&bar[i].x,&bar[i].y);
        }
        int cnt = 0;
        for(int i = 0 ; i < n ; i++) {
            if(inConvexPoly(sheep[i],bar,m) == -1)cnt ++;
        }
        printf("%d\n",cnt);
    }
}

int main(void) {
    //freopen("a.in","r",stdin);
    Deal_with();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值