poj 1066 判断线段相交 思维

来源:互联网 发布:韦德巅峰场均数据 编辑:程序博客网 时间:2024/06/05 08:50

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7175 Accepted: 2946

Description

Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors.
An example is shown below:

Input

The input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).

Output

Print a single line listing the minimum number of doors which need to be created, in the format shown below.

Sample Input

7 20 0 37 100 40 0 76 100 85 0 0 75 100 90 0 90 0 71 100 61 0 14 100 38 100 47 47 100 54.5 55.4 

Sample Output

Number of doors = 2 


题意:

求需要最少破多少墙才能出去(每次破墙要求这么面墙是当前房间的正中间)


转化:

枚举正方形边上的每一个小线段的终点到目的地的连线,可以与多少内部线段相交,再加上一(最外围的墙)




#include<math.h>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 100struct point{    double x,y;    point(){}    point(double _x,double _y){        x=_x,y=_y;    }};point P1[MAXN],P2[MAXN];int k1,k2,k3,k4;point left[MAXN],right[MAXN],up[MAXN],down[MAXN];double Dis(point p1,point p2){    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}double Cross(point p1,point p2,point p3){    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);}point operator - (point A,point B){    return point(A.x-B.x, A.y-B.y);}point operator + (point A, point B){    return point(A.x+B.x, A.y+B.y);}point operator * (point A, double p){    return point(A.x*p, A.y*p);}bool operator == (point A, point B){    return (A.x-B.x) == 0 && (A.y-B.y) == 0;}bool SegmentProperIntersection(point a,point b,point c,point d){    return (max(a.x,b.x)>=min(c.x,d.x))&&            (max(c.x,d.x)>=min(a.x,b.x))&&            (max(a.y,b.y)>=min(c.y,d.y))&&            (max(c.y,d.y)>=min(a.y,b.y))&&            (Cross(a,c,b)*Cross(a,b,d)>=0)&&            (Cross(c,a,d)*Cross(c,d,b)>=0);}int cmp1(const void *x,const void *y){    point *A=(point *)x;    point *B=(point *)y;    return A->y - B->y;}int cmp2(const void *x,const void *y){    point *A=(point *)x;    point *B=(point *)y;    return A->x - B->x;}int n;point key;int deal(point num[],int k){    point t;    int ans=10000,cnt;    for(int i=1;i<k;i++){        cnt=0;        t=(num[i]+num[i-1])*0.5;        for(int j=0;j<n;j++)            if(SegmentProperIntersection(key,t,P1[j],P2[j]))                cnt++;        ans=min(ans,cnt);    }    return ans;}int main(){    //freopen("in.txt","r",stdin);    while(scanf("%d",&n)!=EOF)    {        double a,b,c,d;        k1=k2=k3=k4=0;        for(int i=0;i<n;i++){            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);            P1[i].x=a,P1[i].y=b;            P2[i].x=c,P2[i].y=d;            if(a==0)                left[k1++]=P1[i];            else if(a==100)                right[k2++]=P1[i];            else if(b==100)                up[k3++]=P1[i];            else if(b==0)                down[k4++]=P1[i];            if(c==0)                left[k1++]=P2[i];            else if(c==100)                right[k2++]=P2[i];            else if(d==100)                up[k3++]=P2[i];            else if(d==0)                down[k4++]=P2[i];        }        scanf("%lf%lf",&key.x,&key.y);        left[k1++]=point(0,0),left[k1++]=point(0,100);        right[k2++]=point(100,0),right[k2++]=point(100,100);        up[k3++]=point(0,100),up[k3++]=point(100,100);        down[k4++]=point(0,0),down[k4++]=point(100,0);        qsort(left,k1,sizeof(left[0]),cmp1);        qsort(right,k2,sizeof(right[0]),cmp1);        qsort(up,k3,sizeof(up[0]),cmp2);        qsort(down,k4,sizeof(down[0]),cmp2);        int ans=10000;        ans=min(ans,deal(left,k1)+1);        ans=min(ans,deal(right,k2)+1);        ans=min(ans,deal(up,k3)+1);        ans=min(ans,deal(down,k4)+1);        printf("Number of doors = %d \n",ans);    }    return 0;}


原创粉丝点击