LA 2297 平面直线图(PSLG)

#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-12;
double 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) { }
};

typedef Point Vector;

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;
}
const int maxv = 200 + 5;
const int maxn = 100 + 10;
const double CON = 1E-6;
int V, n;
int G[maxv][maxv], vis[maxv];
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 dfs(int u)
{
	if (u == 1) return true; // 1是终点
	vis[u] = 1;
	for (int v = 0; v < V; v++)
		if (G[u][v] && !vis[v] && dfs(v)) return true;
	return false;
}
bool find_path()
{
	std::vector<Point>vertics;
	vertics.push_back(Point(0, 0));
	vertics.push_back(Point(1e5, 1e5));
	for (int i = 0; i < n; i++)
	{
		if (!OnAnySegment(p1[i])) vertics.push_back(p1[i]);
		if (!OnAnySegment(p2[i])) vertics.push_back(p2[i]);
	}
	V = vertics.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(vertics[i], vertics[j]))
				G[i][j] = G[j][i] = 1;
	return dfs(0);
}
Point read_point()
{
	double x, y;
	scanf("%lf%lf", &x, &y);
	return Point(x, y);
}
int main(int argc, char const *argv[])
{
	while (~scanf("%d", &n) && n)
	{
		for (int i = 0; i < n; i++)
		{
			Point a = read_point();
			Point b = read_point();
			Vector v = b - a;
			v = v / Length(v);
			p1[i] = a - v * CON;
			p2[i] = b + v * CON;
		}
		printf("%s\n", (find_path() ? "no" : "yes"));
	}
	return 0;
}


有点像迷宫,主要解决思路是图论,


建图: 把每一条线段的端点看做一个点,加上起点和终点(无穷远点) ,看起点和无穷远点是否联通即可


建边; 如果两个点的连线没有与其他线段规范相交,就可以认为是联通的,即可连接这条边


注意事项: 墙是有厚度的,(T->A,A->S,但T不能到达S,应为在A处阻隔了) 一个点对应实际两个点,即把相连变为相交,但要注意排除共线的线段(把他们合并为一条线段即可)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值