当前位置:编程学习 > C/C++ >>

POJ 1066 Treasure Hunt

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5048 Accepted: 2099
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
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 
Source
East Central North America 1999
解题思路:将每条线段的端点设为从外面进来的起点,这个点与目标点的连线段中间与哪些线段相交就说明还要穿过几扇门,加上最开始进来的一道门,就是答案,还有进来一扇门都不用过的情况,就将边界的四个点与目标点连起来,同样找交点,然后取上述所有答案的最小值就是最后答案
 
#include<iostream>  
#include<cstdio>  
#include<cstring>  
#include<cmath>  
#define Max 105  
#define eps 1e-8  
using namespace std;  
struct Point     
{    
    double x,y;    
}point[Max];    
struct Seg    
{    
    Point a,b;    
}seg[Max],tseg;    
int dblcmp(double d)      
{      
        if(fabs(d)<eps) return 0;      
        return d>0?1:-1;      
}      
double det(double x1,double y1,double x2,double y2)//area2      
{      
        return x1*y2-x2*y1;      
}      
double cross(Point a,Point b,Point c)      
{      
        return det(b.x-a.x,b.y-a.y,c.x-a.x,c.y-a.y);      
}      
int segcross(Point a,Point b,Point c,Point d)      
{      
        return (dblcmp(cross(a,c,d))^dblcmp(cross(b,c,d)))==-2&&      
                (dblcmp(cross(c,a,b))^dblcmp(cross(d,a,b)))==-2;      
}  
int sum(Seg t,int n)  
{  
    int i,ans=0;  
    for(i=1;i<=n;i++)  
        if(segcross(t.a,t.b,seg[i].a,seg[i].b))  
            ans++;  
    return ans;  
}  
int main()  
{  
    int i,n,ans;  
    while(~scanf("%d",&n))  
    {  
        ans=999;  
        for(i=1;i<=n;i++)  
            scanf("%lf%lf%lf%lf",&seg[i].a.x,&seg[i].a.y,&seg[i].b.x,&seg[i].b.y);  
        scanf("%lf%lf",&tseg.a.x,&tseg.a.y);  
        tseg.b.x=0.0,tseg.b.y=0.0;  
        ans=min(sum(tseg,n),ans);  
        tseg.b.x=0.0,tseg.b.y=100.0;  
        ans=min(sum(tseg,n),ans);  
        tseg.b.x=100.0,tseg.b.y=0.0;  
        ans=min(sum(tseg,n),ans);  
        tseg.b.x=100.0,tseg.b.y=100.0;  
        ans=min(sum(tseg,n),ans);  
        for(i=1;i<=n;i++)  
        {  
            tseg.b.x=seg[i].a.x,tseg.b.y=seg[i].a.y;  
            ans=min(ans,sum(tseg,n));  
            tseg.b.x=seg[i].b.x,tseg.b.y=seg[i].b.y;  
            ans=min(ans,sum(tseg,n));  
        }  
        printf("Number of doors = %d\n",ans+1);  
    }  
    return 0;  
}  

 

 
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,