[ACdream 1215 Get Out!]判断点在封闭图形内

大致题意:在二维平面上,给一些圆形岛屿的坐标和半径,以及圆形船的位置和半径,问能否划到无穷远的地方去

思路:考虑任意两点,如果a和b之间船不能通过,则连一条边,则问题转化为判断点是否在多边形中。先进行坐标变换,将船变到原点,以从原点到每个点的有向角作为状态,每条边的边权为这条边对有向角的改变量,那么点在多边形内相当于存在负权环,用SPFA判负环即可。

#pragma comment(linker, "/STACK:10240000")
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define X                   first
#define Y                   second
#define pb                  push_back
#define mp                  make_pair
#define all(a)              (a).begin(), (a).end()
#define fillchar(a, x)      memset(a, x, sizeof(a))
#define fillarray(a, b)     memcpy(a, b, sizeof(a))

typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;

#ifndef ONLINE_JUDGE
namespace Debug {
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
}
#endif // ONLINE_JUDGE

template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}

const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double EPS = 1e-8;

/* -------------------------------------------------------------------------------- */

const int maxn = 3e2 + 7;

int dcmp(double x) {
    if (fabs(x) < EPS) return 0;
    return x > 0? 1 : - 1;
}

struct Circle {
    double x, y, r;
    Circle(double x, double y, double r) {
        this->x = x;
        this->y = y;
        this->r = r;
    }
    void read() {
        scanf("%lf%lf%lf", &x, &y, &r);
    }
    Circle() {}
};
Circle p[maxn];

double e[maxn][maxn], d[maxn];
bool vis[maxn];
int n, cnt[maxn];

double dist(double x1, double y1, double x2, double y2) {
    return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

bool relax(double u, double w, double &v) {
    if (dcmp(v - u - w) > 0) {
        v = u + w;
        return true;
    }
    return false;
}

bool spfa(int s) {
    queue<int> Q;
    Q.push(s);
    fillchar(vis, 0);
    for (int i = 0; i < n; i ++) {
        d[s] = INF;
    }
    fillchar(cnt, 0);
    d[s] = 0;
    while (!Q.empty()) {
        int H = Q.front(); Q.pop();
        vis[H] = false;
        for (int i = 0; i < n; i ++) {
            if (e[H][i] < INF) {
                if (relax(d[H], e[H][i], d[i]) && !vis[i]) {
                    if (cnt[i] >= n) return true;
                    Q.push(i);
                    vis[i] = true;
                    cnt[i] ++;
                }
            }
        }
    }
    return false;
}

void work() {
    for (int i = 0; i < n; i ++) {
        if (spfa(i)) {
            puts("NO");
            return ;
        }
    }
    puts("YES");
}

double calcangle(int i, int j) {
    Circle a = p[i], b = p[j];
    double angle = acos((a.x * b.x + a.y * b.y) / dist(a.x, a.y, 0, 0) / dist(b.x, b.y, 0, 0));
    if (dcmp(a.x * b.y - a.y * b.x) <= 0) return angle;
    return - angle;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif // ONLINE_JUDGE
    while (cin >> n) {
        for (int i = 0; i < n; i ++) {
            p[i].read();
        }
        Circle me;
        me.read();
        for (int i = 0; i < n; i ++) {
            p[i].x -= me.x;
            p[i].y -= me.y;
        }
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                e[i][j] = INF + 1;
            }
        }
        for (int i = 0; i < n; i ++) {
            for (int j = i + 1; j < n; j ++) {
                double buf = dist(p[i].x, p[i].y, p[j].x, p[j].y) - p[i].r - p[j].r;
                if (dcmp(buf - me.r * 2) < 0) {
                    e[i][j] = calcangle(i, j);
                    e[j][i] = - e[i][j];
                    //Debug::print(i, j, e[i][j]);
                }
            }
        }
        work();
    }
    return 0;
}
时间: 2024-08-04 23:12:53

[ACdream 1215 Get Out!]判断点在封闭图形内的相关文章

判断点是否在多边形内

有一个n边形,顶点为p1,p2,...,pn;给定一个已知点p,判断p在此多边形内还是外. 预备知识: 两线段相交的定义,如果一条线段的两端分别处在另一条线段的两端,则此两线段相交 判断2点在线段的两侧可以用向量的叉乘实现! 基本步骤: 1,过p点垂直向上作一条射线 2,判断此射线与n边形n条边的交点 3,把所有交点相加,如果是奇数则说明在多边形内,否则在多边形外 思路非常的简单,另外说明一下几种特殊的情况: 1,射线与多边形的顶点相交:比如射线过多边形的Pi点,则如果Pi-1和Pi+1在此射线

C# 判断点是否在多边形内

/// <summary>/// 判断点是否在多边形内/// </summary>/// <param name="pnt">点</param>/// <param name="pntlist">区域的点集</param>/// <returns></returns>public static bool PointInFeaces(PointF pnt, List<

AE 判断点是否在面内

1 /// <summary> 2 /// 根据面要素的ID获取面,判断点是否在面内 3 /// </summary> 4 /// <param name="point">要判断的点,射线的起点</param> 5 /// <param name="ID">面的ID</param> 6 /// <param name="pFeatureLayer">面要素所在的图

百度地图 判断marker是否在多边形内

昨天画了圆形,判marker是否存在圆形内.今天来画多边形,判断marker在多边形内. 百度地图API覆盖物多边形类 http://developer.baidu.com/map/reference/index.php?title=Class:%E8%A6%86%E7%9B%96%E7%89%A9%E7%B1%BB/Polygon http://developer.baidu.com/map/reference/index.php?title=Class:%E8%A6%86%E7%9B%96%

判断对象是否在视线内

// Cast a sphere with the desired distance. Check each collider hit to see if it is within the field of view. Set objectFound // to the object that is most directly in front of the agent /// <summary> /// Withins the sight. /// </summary> ///

坐标画图形,发射线判断点是否在图形中

创建一个点的bean public class Point { private double lat; private double lon; public double getLat() { return lat; } public void setLat(double lat) { this.lat = lat; } public double getLon() { return lon; } public void setLon(double lon) { this.lon = lon;

poj1584——判断凸包,判断点是否在多边形内

poj1584——判断凸包,判断点是否在多边形内 A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5441   Accepted: 1729 Description The DIY Furniture company specializes in assemble-it-yourself furniture kits. Typically, the pieces o

js 日期比较大小,js判断日期是否在区间内,js判断时间段是否在另外一个时间段内

/** * 日期解析,字符串转日期 * @param dateString 可以为2017-02-16,2017/02/16,2017.02.16 * @returns {Date} 返回对应的日期对象 */ function dateParse(dateString){ var SEPARATOR_BAR = "-"; var SEPARATOR_SLASH = "/"; var SEPARATOR_DOT = "."; var dateArr

判断点是否在凸多边形内

判断点是否在凸多边形内的方法很多,此处仅给出使用向量叉积判断点是否在凸多边形内的方法. 以下图为例说明问题: 原则: 1. 将多边形的第i条边的第一个顶点指向点P得到向量 v1,然后将从第一个顶点指向第二个顶点得到向量v2,叉乘这两个向量. 2.如果叉乘结果与上一条边的叉乘结果的乘积大于0则继续执行,如果乘积小于0,表示点P不在凸多边形内,直接返回即可. 要点:要求凸多边形的点以固定的顺序给出,例如固定为逆时针或顺时针. 实现的代码如下: struct Point { float x; floa