POJ2318 TOYS 和POJ2398 Toy Storage题解(点在四边形内)(简单几何)

POJ 2318 TOYS 链接:http://poj.org/problem?id=2318

POJ 2398 Toy Storage 链接:http://poj.org/problem?id=2398


poj2318题目大意:给你一个盒子的俯视图,从左到右将每个格子划分为0,1,2...n;给你一些点的坐标,让你输出每个格子里点的个数。

poj2398题目大意:给你一个盒子的俯视图,从左到右将每个格子划分为0,1,2...n;给你一些点的坐标,让你输出所有格子含有i个点的个数有几个(i!=0)。


输入方式:第一行给你盒子有n个隔板有m个点,盒子的左上角(x1,y1)坐标和右下角坐标(x2,y2)。

接下来n行分别是隔板两端的x坐标,在接下来m行是点的坐标。


两题不同之处:POJ2398给的隔板顺序不同,需要排序;输出格式不同;输出内容不同;


POJ2318 AC代码:(红色部分是几何简单模板(参考算法竞赛大白书))

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;

<span style="color:#ff0000;">struct point{
double x,y;
point(double x=0,double y=0):x(x),y(y) { }
};
typedef point V;
struct circle{
point c;
double r;
circle(point c,double r):c(c),r(r){}
point Point(double a){
return point(c.x+cos(a)*r,+c.y+sin(a)*r);
}
};
struct line{
point c;
V v;
};
V operator + (V a,V b){ return V(a.x+b.x,a.y+b.y);}
V operator - (V a,V b){ return V(a.x-b.x,a.y-b.y);}
V operator * (V a,double p){return V(a.x*p,a.y*p);}
V operator / (V a,double p){return V(a.x/p,a.y/p);}
bool operator < (const V& a,const V& b){
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps = 1e-10;
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    else
        return x<0?-1:1;
}
bool operator == (const point& a,const point& b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}

double dot(V a,V b){return a.x*b.x+a.y*b.y; }
double length(V a){return sqrt(dot(a,a));}
double angle(V a,V b){return acos(dot(a,b)/length(a)/length(b)); }
double cross(V a,V b){return a.x*b.y-a.y*b.x; }
double area2(point a,point b,point c){return cross(b-a,c-a); }

/*two line change point返回两线交点(保证有唯一交点)*/
point twolineispoint(point p,V v,point q,V w){
    V u=p-q;
    double t=cross(w,u)/cross(v,w);
    return p+v*t;
}
/*线段是否相交*/
bool segmentmeet(point a1,point a2,point b1,point b2){
    double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),
           c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a1-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
/*点到直线距离*/
point pointtoline(point p,point a,point b){
    V v1=b-a,v2=p-a;
    return fabs(cross(v1,v2))/length(v1);
}</span>
line a[5005];
int ans[5005];
int main(){
    int n,m;
    double strx,stry,endx,endy;
    while(cin>>n&&n){
        cin>>m>>strx>>stry>>endx>>endy;
        memset(ans,0,sizeof(ans));
        a[0].c.x=strx;
        a[0].c.y=stry;
        a[0].v.x=strx;
        a[0].v.y=endy;
        a[n+1].c.x=endx;
        a[n+1].c.y=stry;
        a[n+1].v.x=endx;
        a[n+1].v.y=endy;
        for(int i=1;i<=n;i++){
            double xx,xx2;
            cin>>xx>>xx2;
            a[i].c.x=xx;
            a[i].c.y=stry;
            a[i].v.x=xx2;
            a[i].v.y=endy;
        }
        for(int j=n+1;j<=n+m;j++){
            point p;
            cin>>p.x>>p.y;
            for(int i=0;i<=n;i++){
                if(dcmp((area2(p,a[i].c,a[i].v)))*dcmp((area2(p,a[i+1].c,a[i+1].v)))<0){
                    ans[i]++;
                    break;
                }
            }
        }
        for(int i=0;i<=n;i++){
            cout<<i<<':'<<' '<<ans[i]<<endl;
        }
        cout<<endl;
    }
    return 0;
}

POJ2398 AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;

<span style="color:#ff0000;">struct point{
double x,y;
point(double x=0,double y=0):x(x),y(y) { }
};
typedef point V;
struct circle{
point c;
double r;
circle(point c,double r):c(c),r(r){}
point Point(double a){
return point(c.x+cos(a)*r,+c.y+sin(a)*r);
}
};
struct line{
point c;
V v;
};
V operator + (V a,V b){ return V(a.x+b.x,a.y+b.y);}
V operator - (V a,V b){ return V(a.x-b.x,a.y-b.y);}
V operator * (V a,double p){return V(a.x*p,a.y*p);}
V operator / (V a,double p){return V(a.x/p,a.y/p);}
bool operator < (const V& a,const V& b){
    return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
const double eps = 1e-10;
int dcmp(double x){
    if(fabs(x)<eps) return 0;
    else
        return x<0?-1:1;
}
bool operator == (const point& a,const point& b){
return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}

double dot(V a,V b){return a.x*b.x+a.y*b.y; }
double length(V a){return sqrt(dot(a,a));}
double angle(V a,V b){return acos(dot(a,b)/length(a)/length(b)); }
double cross(V a,V b){return a.x*b.y-a.y*b.x; }
double area2(point a,point b,point c){return cross(b-a,c-a); }

/*two line change point返回两线交点(保证有唯一交点)*/
point twolineispoint(point p,V v,point q,V w){
    V u=p-q;
    double t=cross(w,u)/cross(v,w);
    return p+v*t;
}
/*线段是否相交*/
bool segmentmeet(point a1,point a2,point b1,point b2){
    double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1),
           c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a1-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
/*点到直线距离*/
point pointtoline(point p,point a,point b){
    V v1=b-a,v2=p-a;
    return fabs(cross(v1,v2))/length(v1);
}</span>
line a[5005];
int ans[5005],anss[1005];
int cmp(const line& a,const line& b){
    return a.c<b.c||(a.c==b.c&&a.v<b.v);
}
int main(){
    int n,m;
    double strx,stry,endx,endy;
    while(cin>>n&&n){
        cin>>m>>strx>>stry>>endx>>endy;
        memset(ans,0,sizeof(ans));
        memset(anss,0,sizeof(anss));
        a[0].c.x=strx;
        a[0].c.y=stry;
        a[0].v.x=strx;
        a[0].v.y=endy;
        a[n+1].c.x=endx;
        a[n+1].c.y=stry;
        a[n+1].v.x=endx;
        a[n+1].v.y=endy;
        for(int i=1;i<=n;i++){
            double xx,xx2;
            cin>>xx>>xx2;
            a[i].c.x=xx;
            a[i].c.y=stry;
            a[i].v.x=xx2;
            a[i].v.y=endy;
        }
        sort(a,a+n+1,cmp);
        for(int j=n+1;j<=n+m;j++){
            point p;
            cin>>p.x>>p.y;
            for(int i=0;i<=n;i++){
                if(dcmp((area2(p,a[i].c,a[i].v)))*dcmp((area2(p,a[i+1].c,a[i+1].v)))<0){
                    ans[i]++;
                    break;
                }
            }
        }
        cout<<"Box\n";
        for(int i=0;i<=n;i++){
            if(ans[i]){
                anss[ans[i]]++;
            }
        }
        for(int i=0;i<=n;i++){
            if(anss[i])
            cout<<i<<':'<<' '<<anss[i]<<endl;
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值