题意:
从正方形外围任意一点出发,到达指定点最少通过几次线段?(每次只能从线段中点经过)
题解:思路一:枚举外围起点,连接该点与指定点,相交次数最少的即为答案。思路二:先求出所有线段的中点。以指定点位起点bfs,走到正方形外面的最小步数即是结果。搜索过程中,相邻点(u,v)满足条件:u,v的连线不与其他任何一条线段规范相交。
#include
#include
#include
using std::sort;
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))eps;
}
int same_side(Point p1,Point p2,Point l1,Point l2){
return xmult(l1,p1,l2)*xmult(l1,p2,l2)>eps;
}
//判两点在线段异侧,点在线段上返回0
int opposite_side(Point p1,Point p2,Line l){
return xmult(l.a,p1,l.b)*xmult(l.a,p2,l.b)<-eps;
}
int opposite_side(Point p1,Point p2,Point l1,Point l2){
return xmult(l1,p1,l2)*xmult(l1,p2,l2)<-eps;
}
//判两直线平行
int parallel(Line u,Line v){
return zero((u.a.x-u.b.x)*(v.a.y-v.b.y)-(v.a.x-v.b.x)*(u.a.y-u.b.y));
}
int parallel(Point u1,Point u2, Point v1,Point v2){
return zero((u1.x-u2.x)*(v1.y-v2.y)-(v1.x-v2.x)*(u1.y-u2.y));
}
//判两直线垂直
int perpendicular(Line u,Line v){
return zero((u.a.x-u.b.x)*(v.a.x-v.b.x)+(u.a.y-u.b.y)*(v.a.y-v.b.y));
}
int perpendicular(Point u1,Point u2,Point v1,Point v2){
return zero((u1.x-u2.x)*(v1.x-v2.x)+(u1.y-u2.y)*(v1.y-v2.y));
}
//判两线段相交,包括端点和部分重合
int intersect_in(Line u,Line v){
if (!dots_inLine(u.a,u.b,v.a)||!dots_inLine(u.a,u.b,v.b))
return !same_side(u.a,u.b,v)&&!same_side(v.a,v.b,u);
return dot_onLine_in(u.a,v)||dot_onLine_in(u.b,v)||dot_onLine_in(v.a,u)||dot_onLine_in(v.b,u);
}
int intersect_in(Point u1,Point u2,Point v1,Point v2){
if (!dots_inLine(u1,u2,v1)||!dots_inLine(u1,u2,v2))
return !same_side(u1,u2,v1,v2)&&!same_side(v1,v2,u1,u2);
return dot_onLine_in(u1,v1,v2)||dot_onLine_in(u2,v1,v2)||dot_onLine_in(v1,u1,u2)||dot_onLine_in(v2,u1,u2);
}
//判两线段相交,不包括端点和部分重合
int intersect_ex(Line u,Line v){
return opposite_side(u.a,u.b,v)&&opposite_side(v.a,v.b,u);
}
int intersect_ex(Point u1,Point u2,Point v1,Point v2){
return opposite_side(u1,u2,v1,v2)&&opposite_side(v1,v2,u1,u2);
}
//计算两直线交点,注意事先判断直线是否平行!
//线段交点请另外判线段相交(同时还是要判断是否平行!)
Point intersection(Line u,Line v){
Point ret=u.a;
double t=((u.a.x-v.a.x)*(v.a.y-v.b.y)-(u.a.y-v.a.y)*(v.a.x-v.b.x))
/((u.a.x-u.b.x)*(v.a.y-v.b.y)-(u.a.y-u.b.y)*(v.a.x-v.b.x));
ret.x+=(u.b.x-u.a.x)*t;
ret.y+=(u.b.y-u.a.y)*t;
return ret;
}
Point intersection(Point u1,Point u2,Point v1,Point v2){
Point ret=u1;
double t=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))
/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
ret.x+=(u2.x-u1.x)*t;
ret.y+=(u2.y-u1.y)*t;
return ret;
}
//点到直线上的最近点
Point ptoLine(Point p,Line l){
Point t=p;
t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;
return intersection(p,t,l.a,l.b);
}
Point ptoLine(Point p,Point l1,Point l2){
Point t=p;
t.x+=l1.y-l2.y,t.y+=l2.x-l1.x;
return intersection(p,t,l1,l2);
}
//点到直线距离
double disptoLine(Point p,Line l){
return fabs(xmult(p,l.a,l.b))/distance(l.a,l.b);
}
double disptoLine(Point p,Point l1,Point l2){
return fabs(xmult(p,l1,l2))/distance(l1,l2);
}
double disptoLine(double x,double y,double x1,double y1,double x2,double y2){
return fabs(xmult(x,y,x1,y1,x2,y2))/distance(x1,y1,x2,y2);
}
//点到线段上的最近点
Point ptoseg(Point p,Line l){
Point t=p;
t.x+=l.a.y-l.b.y,t.y+=l.b.x-l.a.x;
if (xmult(l.a,t,p)*xmult(l.b,t,p)>eps)
return distance(p,l.a)eps)
return distance(p,l1)eps)
return distance(p,l.a)eps)
return distance(p,l1) 0;
}
Point p[1000];
Line l[1000];
int main()
{
int n;
Point target, mid;
scanf("%d",&n);
int i, j, c = 0;
for ( i = 0; i < n; i++ )
{
scanf("%lf%lf%lf%lf", &l[i].a.x, &l[i].a.y, &l[i].b.x, &l[i].b.y);
p[c].x = l[i].a.x; p[c++].y = l[i].a.y;
p[c].x = l[i].b.x; p[c++].y = l[i].b.y;
}
scanf("%lf%lf",&target.x, &target.y);
p[c].x = 0; p[c++].y = 0;
p[c].x = 0; p[c++].y = 100;
p[c].x = 100; p[c++].y = 0;
p[c].x = 100; p[c].y = 100;
sort(p,p+c,cmp);
int ans = 99999999, step;
for ( i = 1; i <= c; i++ )
{
mid = getMid(p[i],p[i-1]);
for ( j = step = 0; j < n; j++ )
if ( intersect_ex(mid, target, l[j].a, l[j].b) ) step++;
ans = step < ans ? step : ans;
}
printf("Number of doors = %d\n", ans+1);
return 0;
}