POJ 1066 Treasure

2次AC。只需注意相同端点不用算通过一面墙,最后加上1即可。
//
//  main.cpp
//  Richard
//
//  Created by 邵金杰 on 16/8/8.
//  Copyright © 2016年 邵金杰. All rights reserved.
//


#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
#define INF 1e20
#define EPS 1e-6
struct Point{
    double x,y;
    Point(double x,double y): x(x),y(y) {}
};
struct Vector{
    double x,y;
    Vector(double x,double y): x(x),y(y) {}
};
struct Line{
    Point a,b;
    Line(Point a,Point b): a(a),b(b) {}
};
Vector operator - (Point p,Point q)
{
    return Vector(p.x-q.x,p.y-q.y);
}
Vector operator * (double k,Vector p)
{
    return Vector(k*p.x,k*p.y);
}
Point operator + (Point p,Vector q)
{
    return Point(p.x+q.x,p.y+q.y);
}
bool operator == (Point p,Point q)
{
    return p.x==q.x&&p.y==q.y;
}
bool IsZero(double x)
{
    return -EPS<x&&x<EPS;
}
bool FLessEq(double a,double b)
{
    return b-a>EPS||IsZero(b-a);
}
double cross(Vector p,Vector q)
{
    return p.x*q.y-p.y*q.x;
}
bool intersect(Line l,Line m)
{
    double x=cross(l.a-m.a,m.b-m.a)/2;
    double y=cross(m.b-m.a,l.b-m.a)/2;
    if(IsZero(x+y)) return false;
    if(l.a==m.a||l.a==m.b) return false;
    if(l.b==m.a||l.b==m.b) return false;
    Point d=l.a+(x/(x+y))*(l.b-l.a);
    if(FLessEq(min(l.a.x,l.b.x),d.x)&&FLessEq(d.x,max(l.a.x,l.b.x))
       &&FLessEq(min(l.a.y,l.b.y),d.y)&&FLessEq(d.y,max(l.a.y,l.b.y)))
        return true;
    return false;
}
vector<Point> ve;
vector<Line>  le;
void work(double x,double y)
{
    Point p=Point(x,y);
    int doors=INF;
    int lenv=(int)ve.size();
    int lenl=(int)le.size();
    for(int i=0;i<lenv;i++)
    {
        int cnt=0;
        Line l=Line(p,ve[i]);
        for(int j=0;j<lenl;j++)
        {
            if(intersect(l,le[j])) cnt++;
        }
        doors=min(doors,cnt);
    }
    printf("Number of doors = %d \n",doors+1);
}
int main()
{
    int n;
    double x1,y1,x2,y2,x,y;
    scanf("%d",&n);
    ve.push_back(Point(0,0));
    ve.push_back(Point(100,0));
    ve.push_back(Point(0,100));
    ve.push_back(Point(100,100));
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        ve.push_back(Point(x1,y1));
        ve.push_back(Point(x2,y2));
        le.push_back(Line(Point(x1,y1),Point(x2,y2)));
    }
    scanf("%lf%lf",&x,&y);
    if(n==0) printf("Number of doors = 1 \n");
    else work(x,y);
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值