poj 1066 Treasure Hunt


Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6605 Accepted: 2735

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



题意:金字塔底是一个100*100的正方形(四面四个外墙),其中还有很多内墙(n个),一个内墙从某个外墙出发,到另一个外墙。这样就形成了很多房间,每个房间都没有门。现在知道宝藏的位置,你在金字塔外,需要你钻洞寻宝,要求每次钻洞都必须在线段的中点钻,问最少的打洞次数。


解:枚举入口x,令f(x)=(将入口和宝藏连一条线段,判断与多少内墙相交,然后+1),答案就是miin{f(x)}。

虽然路线是折线,但是判断要穿过多少墙只用关心线段与多少墙相交即可(因为一个内墙连接两个外墙)。



#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define mes(a,x,s)  memset(a,x,(s)*sizeof a[0])
#define mem(a,x)  memset(a,x,sizeof a)
#define ysk(x)  (1<<(x))
typedef long long ll;
typedef pair<int, int> pii;
const int INF =0x3f3f3f3f;
const int maxn=30    ;
int n;

const double PI=cos(-1.0);
const double eps=1e-10;
int dcmp(double x)
{
    if(fabs(x)<eps)  return 0;
    else return x<0?-1:1;
}
struct Point
{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y) {};
    bool operator ==(const Point B)const {return dcmp(x-B.x)==0&&dcmp(y-B.y)==0;}

    bool operator<(const Point& b)const
    {
        return dcmp(x-b.x)<0|| dcmp(x-b.x)==0 &&dcmp(y-b.y)<0;
    }
}a[200],E;
typedef Point Vector;
Vector operator -(Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y); }

Point midPoint(Point A,Point B)
{
    double x=(A.x+B.x)/2;
    double y=(A.y+B.y)/2;
    return Point(x,y);
}

double Cross(Vector A,Vector B)//叉乘
{
    return A.x*B.y-A.y*B.x;
}

bool SegmentProperIntersection(Point a1,Point a2,Point b1,Point b2)//规范相交
{
    double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
           c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
    return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
struct Line
{
    Point p,p2;
    Vector v;
    Line(){}
    Line(Point a,Vector v):p(a),v(v){}//点线式
    void twoPointIntial(Point p,Point p2)//两点式
    {
        this->p=p;
        this->p2=p2;
        v=  p2-p;
    }
}li[maxn+10];
typedef Line Seg;
vector<Point >wall[4];
int na;



void addPoint(double x,double y)
{
    Point p(x,y);
    if(dcmp(y)==0)  wall[0].push_back(p);
    else if(dcmp(x-100)==0 )  wall[1].push_back(p);
    else if(dcmp(y-100)==0)  wall[2].push_back(p);
    else  wall[3].push_back(p);

}

void getPoint()
{
    na=0;
    for0(i,4)
    {
        int siz=wall[i].size();
        for(int j=1;j<siz;j++)
        {
            a[na++]=midPoint(wall[i][j-1],wall[i][j] );
        }
    }
}

int getNumOfIntesect(int ind)
{
    int cnt=0;
    for0(i,n)
    {
        if( SegmentProperIntersection(a[ind],E,li[i].p ,li[i].p2  ) ) cnt++;
    }
    return ++cnt;
}

int solve()
{
    int ans=INF;
    for0(i,na)
    {
        ans=min(ans,getNumOfIntesect(i));
    }
    return ans;
}
int main()
{
   std::ios::sync_with_stdio(false);
   while(cin>>n)
   {
       for0(i,4) wall[i].clear();

       double x[2],y[2];
       Point p[4]={ Point(0,0),Point(100,0) ,Point(100,100),Point(0,100) };
       wall[0].push_back(p[0]);
       wall[0].push_back(p[1]);
       wall[1].push_back(p[1]);
       wall[1].push_back(p[2]);
       wall[2].push_back(p[2]);
       wall[2].push_back(p[3]);
       wall[3].push_back(p[3]);
       wall[3].push_back(p[0]);
       for0(i,n)
       {
           cin>>x[0]>>y[0]>>x[1]>>y[1];
           li[i].twoPointIntial(Point(x[0],y[0]),Point(x[1],y[1]));
           addPoint(x[0],y[0]);
           addPoint(x[1],y[1]);
       }
       cin>>E.x>>E.y;
       for0(i,4)  sort(all(wall[i]));
       getPoint();
       printf("Number of doors = %d\n",solve());

   }
   return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值