欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

POJ 1066 Treasure Hunt

程序员文章站 2022-07-07 22:53:16
...

POJ 1066 Treasure Hunt

##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.
POJ 1066 Treasure Hunt

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 

题目比较新颖,其实就是依次连接正方形四个边上的点与宝藏坐标构成线段,求与之相交的线段的最少数量,注意要特判n=0的情况,AC代码如下:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=100;

struct point{
double x,y;
point(double a=0,double b=0) {x=a; y=b;}
}p[4*N];

struct segment
{
 point s;
 point e;
segment(point a,point b) { s=a; e=b;}
segment() { }
}s[N];

point midpoint(point a,point b){
    point c;
    c.x=(a.x+b.x)/2;
    c.y=(a.y+b.y)/2;
    return c;
}

double cross(point a,point b,point c){
    return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}

bool online(segment l,point p){
    return ((cross(l.e,p,l.s)==0) && (((p.x-l.s.x)*(p.x-l.e.x)<=0) && ((p.y-l.s.y)*(p.y-l.e.y)<=0)));
}

bool intersect(segment u,segment v){
    return ((max(u.s.x,u.e.x)>=min(v.s.x,v.e.x))&&                     //排斥实验
   (max(v.s.x,v.e.x)>=min(u.s.x,u.e.x))&&
   (max(u.s.y,u.e.y)>=min(v.s.y,v.e.y))&&
   (max(v.s.y,v.e.y)>=min(u.s.y,u.e.y))&&
   (cross(v.s,u.e,u.s)*cross(u.e,v.e,u.s)>=0)&&
   (cross(u.s,v.e,v.s)*cross(v.e,u.e,v.s)>=0));
}

bool Intersect(segment u,segment v)
{
 return ((intersect(u,v))&&
   (!online(u,v.s))&&
   (!online(u,v.e))&&
   (!online(v,u.e))&&
   (!online(v,u.s)));
}

int main()
{
    int n,cnt=0,num;
    point treasure;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lf%lf%lf%lf",&s[i].s.x,&s[i].s.y,&s[i].e.x,&s[i].e.y);
        p[cnt++]=s[i].s;
        p[cnt++]=s[i].e;
    }
    scanf("%lf%lf",&treasure.x,&treasure.y);
    int ans=1e9;
    for(int i=0;i<cnt;i++){
        int sum=0;
        segment L;
        L.s=p[i];L.e=treasure;
        for(int j=0;j<n;j++){
            if(Intersect(L,s[j])) sum++;
        }
        ans=min(ans,sum+1);
    }
    if(n==0) printf("Number of doors = 1");
    else printf("Number of doors = %d",ans);
    return 0;
}