代码有点冗余,主要是在计算相邻端点的中点,需要排序等。看网上说法,其实不需要中点,只需要枚举每个端点即可,因为从哪边进入没有区别。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<string>
#include<map>
#include<set>
#include<algorithm>
#include<vector>
#include<sstream>
#include<cmath>
using namespace std;
/**********************************************************/
struct point{
double x,y;
point (double i,double j){x=i;y=j;}//普通构造函数
point (const point& p){x=p.x;y=p.y;}//拷贝构造函数
point& operator = (const point& p){//重载=
x=p.x;y=p.y;
return *this;
}
point operator - (const point& p){//重载-,b-a => b.-(a) 即线段ab
return point (p.x-x,p.y-y);
}
point operator + (const point& p){//重载+,计算中点
return point ((p.x+x)/2,(p.y+y)/2);
}
point (){}
};
struct segment{
point left,right;
segment (point a,point b){left=a;right=b;}
segment (){}
};
const int MAX_NUM = 50;
const double MAX_DOUBLE = 100000000.0;
segment allSegment[MAX_NUM];
vector<point> allPoint[4];
vector<point> allMidPoint;
double CrossMuti (point a,point b){return a.x*b.y-a.y*b.x;}
double min_2 (double x,double y) {return x<y?x:y;}
double max_2 (double x,double y) {return x>y?x:y;}
/* 0-不相交 1-正常相交 2-端点在线段上 3-重合 */
int ab_cross_cd (point a,point b,point c,point d)
{
if(min_2(a.x,b.x)>max_2(c.x,d.x)||min_2(a.y,b.y)>max_2(c.y,d.y)||
max_2(a.x,b.x)<min_2(c.x,d.x)||max_2(a.y,b.y)<min_2(c.y,d.y) ) return 0;
point abVec=b-a,acVec=c-a,adVec=d-a;
double x = CrossMuti (abVec,acVec);
double y = CrossMuti (abVec,adVec);
if(x==0.0&&y==0.0)//重合
return 3;
else if (x==y)
return 0;//不相交
else{
point cdVec=d-c,caVec=a-c,cbVec=b-c;
x*=y;
y=CrossMuti (cdVec,caVec)*CrossMuti (cdVec,cbVec);
if (x<0 && y<0) return 1;
else if ((x==0&&y<=0)||(y==0&&x<=0)) return 2;
else return 0;
}
}
void joinPoint (point& a)
{
if (a.y==0.0)
allPoint[0].push_back (a);
else if (a.x==100.0)
allPoint[1].push_back (a);
else if (a.y==100.0)
allPoint[2].push_back (a);
else if (a.x==0.0)
allPoint[3].push_back (a);
}
bool cmp (point a,point b)
{
if (a.y==0&&b.y==0) return a.x<b.x;
else if (a.x==100.0&&b.x==100.0) return a.y<b.y;
else if (a.y==100.0&&b.y==100.0) return a.x>b.x;
else if (a.x==0&&b.x==0) return a.y>b.y;
}
void clearPoint ()
{
for (int i=0;i<4;i++)
allPoint[i].clear ();
}
/**********************************************************/
int main()
{
//freopen ("in.txt","r",stdin);
int n;
point treasure;
while (scanf ("%d",&n)!=EOF)
{
allPoint[0].push_back (point (0.0,0.0));
allPoint[1].push_back (point (100.0,0.0));
allPoint[2].push_back (point (100.0,100.0));
allPoint[3].push_back (point (0.0,100.0));
for (int i=0;i<n;i++){
scanf ("%lf %lf %lf %lf",&allSegment[i].left.x,&allSegment[i].left.y,&allSegment[i].right.x,&allSegment[i].right.y);
joinPoint (allSegment[i].left);
joinPoint (allSegment[i].right);
}
scanf ("%lf %lf",&treasure.x,&treasure.y);
for (int i=0;i<4;i++)
sort (allPoint[i].begin (),allPoint[i].end (),cmp);
allPoint[0].push_back (point (100.0,0.0));
allPoint[1].push_back (point (100.0,100.0));
allPoint[2].push_back (point (0.0,100.0));
allPoint[3].push_back (point (0.0,0.0));
allSegment[n++]=segment (point (0,0),point (100.0,0));
allSegment[n++]=segment (point (100.0,0),point (100.0,100.0));
allSegment[n++]=segment (point (100.0,100.0),point (0,100.0));
allSegment[n++]=segment (point (0,100.0),point (0,0));
/*for (int i=0;i<4;i++){
for (int j=0;j<allPoint[i].size ();j++)
printf ("(%.2lf,%.2lf) ",allPoint[i][j].x,allPoint[i][j].y);
printf ("\n");
}*/
int minNum=100000,id1,id2;
for (int i=0;i<4;i++){
for (int j=0;j+1<allPoint[i].size ();j++){
point midPoint=allPoint[i][j]+allPoint[i][j+1];
int sum=0;
for (int k=0;k<n;k++)
if (ab_cross_cd (midPoint,treasure,allSegment[k].left,allSegment[k].right)>0)
sum++;
if (sum && minNum>sum) {
minNum=sum;
id1=i,id2=j;
}
}
}
//printf ("%lf %lf\n",allPoint[id1][id2].x,allPoint[id1][id2].y);
printf ("Number of doors = %d\n",minNum);
clearPoint ();
}
return 0;
}