Treasure Hunt POJ - 1066

Treasure Hunt POJ - 1066
分类:计算几何+线段相交

题目:
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.

题意:给定区域,并给出线段划分了改区域。问从外面出发,最小需要经过几座墙可以到达终点。

思路: 从边界的点与终点构成一个线段,记录经过与给的线段交点个数,输出最小个数。此处注意要判断四个顶点与终点构成的线段。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<stdlib.h>
using namespace std;
const int maxn=33;
const int inf=0x7f7f7f7f;
struct  point
{
    double x,y;
    point(){}
    point(double _x,double _y){x=_x;y=_y;}
};
struct egde
{
    point start,end;//
    egde(){}
    egde(point a,point b){
       start=a;end=b;}
}edges[maxn];
double multi(point p1,point p2,point p0)
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool intAcross(egde v1,egde v2)
{
    if(max(v1.start.x,v1.end.x)>=min(v2.start.x,v2.end.x)&&
        max(v2.start.x,v2.end.x)>=min(v1.start.x,v1.end.x)&&
        max(v1.start.y,v1.end.y)>=min(v2.start.y,v2.end.y)&&
        multi(v2.start,v1.end,v1.start)*multi(v1.end,v2.end,v1.start)>0&&
        multi(v1.start,v2.end,v2.start)*multi(v2.end,v1.end,v2.start)>0) return 1;//相交
    return 0;
}
int main()
{
    int n;
    double a,b,c,d;
    point boss; 
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
        edges[i]=egde(point(a,b),point(c,d));
    }
    scanf("%lf%lf",&boss.x,&boss.y);
    int ans=inf;
    for(int i=0;i<n;i++)
    {
        int cnt=0;
        egde left=egde(boss,edges[i].start);
        egde right=egde(boss,edges[i].end);
        for(int j=0;j<n;j++)
           if(i!=j&&intAcross(left,edges[j])) cnt++; 
        //cout<<"left "<<cnt<<endl;
        ans=min(ans,cnt+1);
        cnt=0;
        for(int j=0;j<n;j++)
            if(i!=j&&intAcross(right,edges[j])) cnt++;
        ans=min(ans,cnt+1);
    }
    egde dd[5];
    dd[1]=egde(boss,point(0,0)); dd[2]=egde(boss,point(100,0));
    dd[3]=egde(boss,point(100,100));dd[4]=egde(boss,point(0,100));
    for(int i=1;i<5;i++)
    {
        int cnt=0;
        for(int j=0;j<n;j++)
            if(intAcross(dd[i],edges[j])) cnt++;
        ans=min(ans,cnt+1);
    }
    printf("Number of doors = %d \n",ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值