版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lidengdengter/article/details/82049023
四堵墙围成的正方形内有n堵墙,求从正方形外进入宝藏处需要破墙次数的最小值。(每次破墙只能从中点破)。
简单想就是从正方形边界找点到宝藏处会与多少条线段(墙)相交,再加上打破边界的这堵墙,求个最小值即可,判断相交用交叉积。
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=100;
int n;
struct point{
double x,y;
point(){}
point(double sx,double sy):x(sx),y(sy){ };
point operator-(const point &w)const{
return point(x-w.x,y-w.y);
}
double operator*(const point &w)const{
return x*w.x+y*w.y;
}
double operator^(const point &w)const{
return x*w.y-y*w.x;
}
}p[N],ed;
int work(point a,point b){
int res=0;
if(a.x==b.x&&a.y==b.y)
return 0;
for(int i=1;i<=n;i++){
double res1=((a-p[i])^(a-b))*((a-p[i+n])^(a-b));
double res2=((p[i]-a)^(p[i]-p[i+n]))*((p[i]-b)^p[i]-p[i+n]);
if(res1<=0&&res2<=0)
res++;
}
return res;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i+n].x,&p[i+n].y);
scanf("%lf%lf",&ed.x,&ed.y);
if(n==0){
printf("Number of doors = 1\n");
return 0;
}
int ans=1000;
for(int i=1;i<=2*n;i++)
ans=min(ans,work(p[i],ed));
printf("Number of doors = %d\n",ans);
return 0;
}