就是将把外围的四条边分割,然后取每段的中点与treasure相连,求出其中所交wall的数量最少的即可。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
const double eps=1e-6;
int corner[4][2]={{0,0},{0,100},{100,0},{100,100}};
int dcmp(double x)
{
if(fabs(x)<eps) return 0;
else return x<0?-1:1;
}
struct point
{
double x,y;
point(double x=0,double y=0):x(x),y(y){}
};
point operator-(point a,point b){return point(a.x-b.x,a.y-b.y);}
bool operator<(const point& a,const point& b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
double cross(point a,point b){return a.x*b.y-a.y*b.x;}
bool seginter(point a1,point a2,point b1,point b2)
{
double c1=cross(a2-a1,b1-a1),c2=cross(a2-a1,b2-a1);
double c3=cross(b2-b1,a1-b1),c4=cross(b2-b1,a2-b1);
return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
vector<point> edge[4];
point p[50][2];
int vis[4];
point s,e;
int n;
int main()
{
int T;
cin>>T;
while(T--)
{
for(int i=0;i<4;i++) edge[i].clear();
cin>>n;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<2;j++)
{
cin>>p[i][j].x>>p[i][j].y;
if(p[i][j].x==0) edge[0].push_back(p[i][j]);
if(p[i][j].x==100) edge[1].push_back(p[i][j]);
if(p[i][j].y==0) edge[2].push_back(p[i][j]);
if(p[i][j].y==100) edge[3].push_back(p[i][j]);
for(int k=0;k<4;k++) if(corner[k][0]==p[i][j].x&&corner[k][1]==p[i][j].y)
vis[k]=1;
}
}
cin>>e.x>>e.y;
for(int i=0;i<4;i++)
{
if(vis[i]) continue;
s.x=corner[i][0];s.y=corner[i][1];
if(s.x==0) edge[0].push_back(s);
if(s.x==100) edge[1].push_back(s);
if(s.y==0) edge[2].push_back(s);
if(s.y==100) edge[3].push_back(s);
}
for(int i=0;i<4;i++) sort(edge[i].begin(),edge[i].end());
int ans=1000000;
for(int i=0;i<4;i++)
for(int j=0;j<edge[i].size()-1;j++)
{
s.x=(edge[i][j].x+edge[i][j+1].x)/2;
s.y=(edge[i][j].y+edge[i][j+1].y)/2;
int tmp=1;
for(int k=0;k<n;k++)
{
if(seginter(s,e,p[k][0],p[k][1])) tmp++;
}
ans=min(ans,tmp);
}
cout<<"Number of doors = "<<ans<<endl;
if(T) cout<<endl;
}
return 0;
}