码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1066 Treasure Hunt(计算几何)

时间:2015-08-06 02:04:39      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:poj 1066 treasure hu   计算几何   

Treasure Hunt
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5857   Accepted: 2439

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


题意:一个正方形围墙内有一些交错的内墙,内墙的端点都在正方形上,在正方形内部有一个点,求从正方形外到这个点的最少要走的门数,门只能是          线段的重点。

题解:枚举 四道墙上的门,门上的中点与起始点的连线与所有线段相交最少的即为答案。

#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<vector>
#define N 100010
#define inf 99999999999.0

using namespace std;

struct Point {
    double x,y;
} ;

struct Line {
    Point p1,p2;
} ;

int n,num;
bool vis[N];
vector<Line>vec;
vector<double>p[4];

Point addPoint(double x,double y) {
    Point it;
    it.x=x;
    it.y=y;
    return it;
}

void addLine(double x1,double y1,double x2,double y2) {
    Point a=addPoint(x1,y1);
    Point b=addPoint(x2,y2);
    Line it;
    it.p1=a,it.p2=b;
    vec.push_back(it);
}

///叉积
double multi(Point p0,Point p1,Point p2) {
    return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}

///线段相交判断
bool is_inter(Point s1,Point e1,Point s2,Point e2) {
    return (max(s1.x,e1.x)>=min(s2.x,e2.x))&&
           (max(s2.x,e2.x)>=min(s1.x,e1.x))&&
           (max(s1.y,e1.y)>=min(s2.y,e2.y))&&
           (max(s2.y,e2.y)>=min(s1.y,e1.y))&&
           (multi(s1,s2,e1)*multi(s1,e1,e2)>=0)&&
           (multi(s2,s1,e2)*multi(s2,e2,e1)>=0);
}

///四条边上的点
void p_push(double x,double y) {
    if(x==0)  p[0].push_back(y);
    else if(x==100)  p[1].push_back(y);
    else if(y==0)  p[2].push_back(x);
    else   p[3].push_back(x);
}

int main() {
    // freopen("test.in","r",stdin);
    while(cin>>n) {
        vec.clear();
        for(int i=0; i<4; i++)p[i].clear();
        double sx,sy,ex,ey;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf%lf%lf",&sx,&sy,&ex,&ey);
            addLine(sx,sy,ex,ey);
            p_push(sx,sy);
            p_push(ex,ey);
        }
        Point t;
        scanf("%lf%lf",&t.x,&t.y);
        int Min=1000000;
        int sum=0;
        for(int i=0; i<4; i++) {
            p[i].push_back(0.0);
            p[i].push_back(100.0);
            sort(p[i].begin(),p[i].end());
            sum=0;
            Point it;
            for(int j=1; j<p[i].size(); j++) {
                if(i<2) {                        ///在两纵向墙上
                    it.y=(p[i][j]+p[i][j-1])/2.0;
                    it.x=(i==0)?0.0:100.0;
                } else {                          ///在横向墙上
                    it.x=(p[i][j]+p[i][j-1])/2.0;
                    it.y=(i==2)?0.0:100.0;
                }
                ///与所有的线段相交的个数
                sum=0;
                for(int k=0; k<vec.size(); k++) {
                    if(is_inter(t,it,vec[k].p1,vec[k].p2))sum++;
                }
                Min=min(sum,Min);
            }
        }
        printf("Number of doors = %d\n",Min+1);///加上最后一道墙
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1066 Treasure Hunt(计算几何)

标签:poj 1066 treasure hu   计算几何   

原文地址:http://blog.csdn.net/acm_baihuzi/article/details/47307437

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!