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

POJ 1066 Treasure Hunt --几何,线段相交

时间:2014-11-19 22:01:27      阅读:527      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

题意: 正方形的房子,给一些墙,墙在区域内是封闭的,给你人的坐标,每穿过一道墙需要一把钥匙,问走出正方形需要多少把钥匙。

解法: 因为墙是封闭的,所以绕路也不会减少通过的墙的个数,还不如不绕路走直线,所以枚举角度,得出直线,求出与正方形内的所有墙交点最少的值,最后加1(正方形边界)。

代码:

bubuko.com,布布扣
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define pi acos(-1.0)
#define eps 1e-8
using namespace std;
#define N 100017

struct Point{
    double x,y;
    Point(double x=0, double y=0):x(x),y(y) {}
    void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
struct Circle{
    Point c;
    double r;
    Circle(){}
    Circle(Point c,double r):c(c),r(r) {}
    Point point(double a) { return Point(c.x + cos(a)*r, c.y + sin(a)*r); }
    void input() { scanf("%lf%lf%lf",&c.x,&c.y,&r); }
};
struct Line{
    Point p;
    Vector v;
    double ang;
    Line(){}
    Line(Point p, Vector v):p(p),v(v) { ang = atan2(v.y,v.x); }
    Point point(double t) { return Point(p.x + t*v.x, p.y + t*v.y); }
    bool operator < (const Line &L)const { return ang < L.ang; }
};
int dcmp(double x) {
    if(x < -eps) return -1;
    if(x > eps) return 1;
    return 0;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
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 a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return 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; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }
Vector VectorUnit(Vector x){ return x / Length(x);}
Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);}
double angle(Vector v) { return atan2(v.y, v.x); }

bool SegmentIntersection(Point A,Point B,Point C,Point D) {
    if(dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) <= 0 && dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) <= 0) return true;
    return false;
}
//data segment
struct Seg{
    Point P[2];
}seg[45];
//data ends

int main()
{
    int n,m,i,j;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        seg[i].P[0].input(), seg[i].P[1].input();
    Point C,D;
    C.input();
    int Mini = Mod;
    double delta = 2*pi*0.001;
    for(i=1;i<=1000;i++)
    {
        double ang = delta*i;
        D.x = 10000.0*cos(ang) + C.x;
        D.y = 10000.0*sin(ang) + C.y;
        int cnt = 0;
        for(j=1;j<=n;j++)
            if(SegmentIntersection(seg[j].P[0],seg[j].P[1],C,D))
                cnt++;
        Mini = min(Mini,cnt);
    }
    printf("Number of doors = %d\n",Mini+1);
    return 0;
}
View Code

 

POJ 1066 Treasure Hunt --几何,线段相交

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/whatbeg/p/4109240.html

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