题意 :从边上出发 找到最少的路径去取得宝藏 就是开最少了门 计算几何 枚举线段与直线相交
枚举的线段是所有输入的点 包括墙的四个角的点 直线也一样 最后找出最小的相交次数 减一就是要开的最少门
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define eps 1e-8
#define Max 35
using namespace std;
typedef struct
{
double x,y;
}point;
point p[Max*2];
int p_len;
typedef struct
{
point a,b;
}Line;
Line l[Max + 5];
int l_len;
double Multi(point p1, point p2, point p0)//叉积
{
return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}
/*直线与线段的相交判断在线段的端点相交也是相交*/
bool Across(point a, point b, point c, point d)
{
double tmp = Multi(c, b, a) * Multi(d, b, a);
if (tmp < 0 || fabs(tmp) < eps) return true;
return false;
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int T;
while(scanf("%d",&T) != EOF)
{
p[0].x = 0;p[0].y = 0;
p[1].x = 0;p[1].y = 100;
p[2].x = 100;p[2].y = 100;
p[3].x = 100;p[3].y = 0;
l[0].a = p[0];l[0].b = p[1];
l[1].a = p[1];l[1].b = p[2];
l[2].a = p[2];l[2].b = p[3];
l[3].a = p[3];l[3].b = p[0];
l_len = 4;
p_len = 2 * T + 4;
for(int i = 4; i < 2*T+4; i+=2)
{
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+1].x,&p[i+1].y);
l[l_len].a = p[i];l[l_len++].b = p[i+1];
}
point s;
scanf("%lf%lf",&s.x,&s.y);
//枚举所有点和treasure点组成的线段
int minn = 1000;
for(int i = 0; i < p_len; i++)
{
int cnt = 0;
for(int j = 0; j < l_len; j++)//直线与线段的相交次数
if(Across(l[j].a,l[j].b,p[i],s))cnt++;
minn = min(minn,cnt);
}
printf("Number of doors = %d\n",minn - 1);//因为是以端点枚举多了一条相交的
}
return 0;
}