Poj-1066 (Determine whether the line segment and the line segment intersect)

topic

http://poj.org/problem?id=1066

analysis

It seems that you only need to enumerate the points that appear, connect them to P to form a line segment, and then see how many intersections with other "walls", the smallest number of intersections is the answer.

program

#include <cstdio>
int n,ans=100;
struct node{double x,y;} U[50],V[50],P;
double cheng(node A,node B){
   
   return (A.x*B.y)-(B.x*A.y);}
node vct(node A,node B){
   
   return (node){B.x-A.x,B.y-A.y};}
void zzk(node X,node Y){
    int ret=0;
    double k1,k2,k3,k4;
    for (int i=1; i<=n; i++){
        k1=cheng(vct(X,Y),vct(X,U[i]));
        k2=cheng(vct(X,Y),vct(X,V[i]));
        k3=cheng(vct(U[i],V[i]),vct(U[i],X));
        k4=cheng(vct(U[i],V[i]),vct(U[i],Y));
        if (k1*k2<=0 && k3*k4<=0) ret++;
    }
    if (ret<ans) ans=ret;
}

int main(){

    scanf("%d",&n);
    if (n==0) ans=1;    //要是没有线段,那么只用炸最外面一个墙即可 
    for (int i=1; i<=n; i++) scanf("%lf%lf%lf%lf",&U[i].x,&U[i].y,&V[i].x,&V[i].y);
    scanf("%lf%lf",&P.x,&P.y);
    for (int i=1; i<=n; i++){
        zzk(P,U[i]);
        zzk(P,V[i]);
    }
    printf("Number of doors = %d",ans);
}

prompt

Note that if n==0, a special judgment is required, and 1 is output.

Guess you like

Origin blog.csdn.net/jackypigpig/article/details/78680468