题意:现在有一个方框,有n条线段分割这个方框,线段的断点均在方框的边上,求从方框外面到方框内某一点所穿过的最少的线段数,过两线段交点记作一条。
思路:两线段规范相交的模板题。
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#define LL long long
#define PI (acos(-1))
#define EPS (1e-4)
using namespace std;
struct P
{
double x,y;
}p[80];
struct L
{
P p1,p2;
}line[50];
double X_Mul(P a1,P a2,P b1,P b2)
{
P v1 = {a2.x-a1.x,a2.y-a1.y},v2 = {b2.x-b1.x,b2.y-b1.y};
return v1.x*v2.y - v1.y*v2.x;
}
double Cal_Point_Dis(P p1,P p2)
{
return sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
}
P Cal_Segment_Cross_Point(P a1,P a2,P b1,P b2)
{
double t = X_Mul(b1,b2,b1,a1) / X_Mul(a1,a2,b1,b2);
P cp = {a1.x+(a2.x-a1.x)*t,a1.y+(a2.y-a1.y)*t};
return cp;
}
bool Is_Seg_Cross(P a1,P a2,P b1,P b2)
{
double xm1 = X_Mul(a1,a2,a1,b1);
double xm2 = X_Mul(a1,a2,a1,b2);
double xm3 = X_Mul(b1,b2,b1,a1);
double xm4 = X_Mul(b1,b2,b1,a2);
return (xm1*xm2 < (-EPS) && xm3*xm4 < (-EPS));
}
int Cal_Minimum_Doors(L *line,int n,P tr,P *p,int top)
{
int i,j;
int Min = 10000000,temp;
for(i = 0;i < top; ++i)
{
for(j = 0,temp = 1;j < n; ++j)
{
if(Is_Seg_Cross(tr,p[i],line[j].p1,line[j].p2))
{
temp++;
}
}
Min = (Min < temp ? Min : temp);
}
return Min;
}
int main()
{
int top,n,i;
P tr;
while(scanf("%d",&n) != EOF)
{
top = 0;
for(i = 0; i < n; ++i)
{
scanf("%lf %lf %lf %lf",&line[i].p1.x,&line[i].p1.y,&line[i].p2.x,&line[i].p2.y);
p[top++] = line[i].p1;
p[top++] = line[i].p2;
}
p[top].x = 0;
p[top++].y = 0;
p[top].x = 0;
p[top++].y = 100;
p[top].x = 100;
p[top++].y = 0;
p[top].x = 100;
p[top++].y = 100;
scanf("%lf %lf",&tr.x,&tr.y);
printf("Number of doors = %d\n",Cal_Minimum_Doors(line,n,tr,p,top));
}
return 0;
}