http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=4064

思路:微延长每条线段,这样检测区域的封闭性就可以看各个端点之间能否直接相连,最后看原点(怪物位置)能否dfs达到一个坐标很大的点。
注意:若延长后的端点在任意一条线段上则忽略该点。(因为微延长的技巧是为了造成“墙”的效果,且“墙”中间是不能有点的)

/*0.062s*/

#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-12;

struct Point
{
    double x, y;
    Point(double x = 0, double y = 0): x(x), y(y) {}
};
typedef Point Vector;

double dcmp(double x)
{
    if (fabs(x) < eps) return 0;
    else return x < 0 ? -1 : 1;
}

Vector operator + (const Point& A, const Point& B)
{
    return Vector(A.x + B.x, A.y + B.y);
}

Vector operator - (const Point& A, const Point& B)
{
    return Vector(A.x - B.x, A.y - B.y);
}

Vector operator * (const Point& A, double v)
{
    return Vector(A.x * v, A.y * v);
}

Vector operator / (const Point& A, double v)
{
    return Vector(A.x / v, A.y / v);
}

double Cross(const Vector& A, const Vector& B)
{
    return A.x * B.y - A.y * B.x;
}

double Dot(const Vector& A, const Vector& B)
{
    return A.x * B.x + A.y * B.y;
}

double Length(const Vector& A)
{
    return sqrt(Dot(A, A));
}

bool operator < (const Point& p1, const Point& p2)
{
    return p1.x < p2.x || (p1.x == p2.x && p1.y < p2.y);
}

bool operator == (const Point& p1, const Point& p2)
{
    return p1.x == p2.x && p1.y == p2.y;
}

bool SegmentProperIntersection(const Point& a1, const Point& a2, const Point& b1, const 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;
}

bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
    return dcmp(Cross(a1 - p, a2 - p)) == 0 && dcmp(Dot(a1 - p, a2 - p)) < 0;
}
///模板

///dfs
const int maxv = 205;

int V;
bool G[maxv][maxv], vis[maxv];

bool dfs(int u)
{
    if (u == 1) return true; /// 1是终点
 vis[u] = true;
    for (int v = 0; v < V; v++)
        if (G[u][v] && !vis[v] && dfs(v)) return true;
    return false;
}
///dfs

const int maxn = 105;
int n;
Point p1[maxn], p2[maxn];

/// 在任何一条线段的中间(在端点不算)
bool OnAnySegment(Point p)
{
    for (int i = 0; i < n; i++)
        if (OnSegment(p, p1[i], p2[i])) return true;
    return false;
}

/// 与任何一条线段规范相交
bool IntersectWithAnySegment(Point a, Point b)
{
    for (int i = 0; i < n; i++)
        if (SegmentProperIntersection(a, b, p1[i], p2[i])) return true;
    return false;
}

bool find_path()
{
    /// 先构图
 vector<Point> vertices;
    vertices.push_back(Point(0, 0)); /// 起点
 vertices.push_back(Point(1e5, 1e5)); /// 终点
 for (int i = 0; i < n; i++)
    {
        if (!OnAnySegment(p1[i])) vertices.push_back(p1[i]);
        if (!OnAnySegment(p2[i])) vertices.push_back(p2[i]); ///端点不能在墙上
 }
    V = vertices.size();
    memset(G, 0, sizeof(G));
    memset(vis, 0, sizeof(vis));
    for (int i = 0; i < V; i++)
        for (int j = i + 1; j < V; j++)
            if (!IntersectWithAnySegment(vertices[i], vertices[j]))
                G[i][j] = G[j][i] = true;
    return dfs(0);
}

int main()
{
    while (cin >> n, n)
    {
        for (int i = 0; i < n; i++)
        {
            double x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            Point a = Point(x1, y1);
            Point b = Point(x2, y2);
            Vector v = b - a;
            v = v / Length(v); ///单位化
         p1[i] = a - v * 1e-6;
            p2[i] = b + v * 1e-6;
        }
        if (find_path()) cout << "no\n";
        else cout << "yes\n";
    }
    return 0;
}

Comments

comments powered by Disqus