POJ-1066Treasure Hunt(计算几何线段相交)

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7319 Accepted: 3024
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
Source

East Central North America 1999

题意:

给N条线,形成了很多线段,在给定一个点,每次只能从线段的中点通过,问走出这个正方形最小需要经过几扇门。

猜结论,枚举终点位置,与出发点连线,和线段相交的数量就是需要通过的门数,最后取min即可。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const double eps=1e-10;
const int maxn=100+5;

struct point
{
  double x,y;
  point(double x=0,double y=0):x(x),y(y){};
  void IN(){scanf("%lf%lf",&x,&y);}
};

int dcmp(double x){if(fabs(x)<eps)return 0;return x>0?1:-1;}
point operator + (const point &a,const point &b){return point(a.x+b.x,a.y+b.y);}
point operator - (const point &a,const point &b){return point(a.x-b.x,a.y-b.y);}
point operator * (const point &a,double t){return point(a.x*t,a.y*t);}
point operator / (const point &a,double t){return point(a.x/t,a.y/t);}
double operator * (const point &a,const point &b){return a.x*b.x+a.y*b.y;}
double operator ^ (const point &a,const point &b){return a.x*b.y-b.x*a.y;} 
double Len(const point &a){return sqrt(a*a);}
double Angle(const point &a,const point &b){return acos(a*b/Len(a)/Len(b));}

point rotate(const point &a,double rad){return point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}

bool operator <(const point &a,const point &b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}
bool operator ==(const point &a,const point &b){return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;}

struct Line
{
  point st,ed;
  Line(){};
  Line(point a,point b):st(a),ed(b){}
  void IN(){st.IN();ed.IN();}
};

bool isIntersected(Line a,Line b)
{
  return (min(a.st.x,a.ed.x)<=max(b.st.x,b.ed.x))&&
         (min(b.st.x,b.ed.x)<=max(a.st.x,a.ed.x))&&
         (min(a.st.y,a.ed.y)<=max(b.st.y,b.ed.y))&&
         (min(b.st.y,b.ed.y)<=max(a.st.y,a.ed.y))&&
         (dcmp((a.st-a.ed)^(b.st-a.st))*dcmp((a.st-a.ed)^(b.ed-a.st))<=0)&&
         (dcmp((b.st-b.ed)^(a.st-b.st))*dcmp((b.st-b.ed)^(a.ed-b.st))<=0);
}

int n;
vector<point>p[4];
Line a[maxn];

inline void add(point &a)
{
  if(a.y==0)p[0].push_back(a);
  else if(a.y==100)p[1].push_back(a);
  else if(a.x==100)p[2].push_back(a);
  else if(a.x==0)p[3].push_back(a);
}

int main()
{
  while(~scanf("%d",&n))
  {
    for(int i=0;i<4;++i)p[i].clear();
    for(int i=1;i<=n;++i)
    {
      a[i].IN();
      add(a[i].st);add(a[i].ed);
    }

    p[0].push_back(point(0,0));
    p[0].push_back(point(100,0));

    p[1].push_back(point(0,100));
    p[1].push_back(point(100,100));

    p[2].push_back(point(100,0));
    p[2].push_back(point(100,100));

    p[3].push_back(point(0,0));
    p[3].push_back(point(0,100));

    point des;
    des.IN();
    int ans=1<<30;
    for(int i=0;i<4;++i)
    {
      sort(p[i].begin(),p[i].end());
      point it;
      for(int j=1;j<p[i].size();++j)
      {
        it.x=(p[i][j].x+p[i][j-1].x)/2.0;
        it.y=(p[i][j].y+p[i][j-1].y)/2.0;

        int sum=0;
        Line now;
        now=Line(it,des);
        for(int j=1;j<=n;++j)
        if(isIntersected(now,a[j]))++sum;
        ans=min(ans,sum);
      }
    }
    printf("Number of doors = %d\n",ans+1);
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值