题意:在一个正方形中有多条线段,线段的端点都在正方形的边上,有一个点,然后求从边上到该点最少需要穿过几条线段。
思路:和在一群线段里找一条直线,它穿过所有的线段,而这条直线一定过两条线段的端点,所以可以试想该线段一定是正方形四周的一个点,所以遍历四周的线段的点,找到最小的相交次数。
注意:当n == 0 的时候我刚开始直接输出结果了,其实还要再读一个点!!!!wa哭了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
double x,y;
Point(double x = 0,double y = 0):x(x),y(y){}
};
typedef Point Vector;
Vector operator + (Vector a, Vector b) { return Vector(a.x+b.x,a.y+b.y) ;}
Vector operator - (Vector a, Vector b) { return Vector(a.x-b.x,a.y-b.y) ;}
Vector operator * (Vector a,double p) { return Vector(a.x*p,a.y*p) ;}
Vector operator / (Vector a,double p) { return Vector(a.x/p,a.y/p) ;}
double Dot(Vector a,Vector b) { return a.x*b.x + a.y*b.y ;}
double Length(Vector a) { return sqrt(Dot(a,a)) ;}
double Cross(Vector a, Vector b) { return a.x*b.y - a.y*b.x ;}
const double eps = 1e-5;
int dcmp(double x)
{
if(fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
}
bool operator == (Point a,Point b)
{
return dcmp(a.x-b.x) == 0&& dcmp(a.y-b.y) == 0;
}
bool operator < (Point a,Point b)
{
return a.x < b.x || (a.x == b.x && a.y < b.y);
}
bool SegmentIntersection(Point a,Point b,Point c,Point d)
{
double d1 = Cross(b-a,c-a),d2 = Cross(b-a,d-a),d3 = Cross(d-c,a-c),d4 = Cross(d-c,b-c);
return dcmp(dcmp(d1)*dcmp(d2)) < 0 && dcmp(dcmp(d3)*dcmp(d4)) < 0;
}
struct Line
{
Point s,e;
Line(Point s = 0,Point e = 0) :s(s),e(e){}
};
bool OnIntersection(Point a)
{
Point p[4];
p[0] = Point(0,0),p[1] = Point(0,100),p[2] = Point(100,100),p[3] = Point(100,0);
for(int i = 0; i < 4; i++)
{
if(a == p[i])
return false;
}
return true;
}
int main()
{
int n;
Line L[100];
while(scanf("%d",&n) != EOF )
{
for(int i = 0; i < n; i++)
{
scanf("%lf%lf%lf%lf",&L[i].s.x,&L[i].s.y,&L[i].e.x,&L[i].e.y);
}
Point M;
scanf("%lf%lf",&M.x,&M.y);
int MIN = 99999;
for(int i = 0; i < n; i++)
{
int ssum = 0,esum = 0;
for(int j = 0; j < n; j++)
{
if(OnIntersection(L[i].s) && SegmentIntersection(L[i].s,M,L[j].s,L[j].e) )
{
ssum++;
}
if(OnIntersection(L[i].e) && SegmentIntersection(L[i].e,M,L[j].s,L[j].e) )
{
esum++;
}
}
if(ssum < MIN)
{
MIN = ssum;
}
if(esum < MIN)
{
MIN = esum;
}
}
if(n == 0)
{
printf("Number of doors = 1\n");
}
else
printf("Number of doors = %d\n",MIN+1);
}
return 0;
}