LA 2797 平面区域dfs
题目大意:一个平面区域有n条线段,问能否从(0,0)处到达无穷远处(不穿过任何线段)
分析:若两条线段有一个端点重合,这种情况是不能从端点重合处穿过的 的。因此对每个端点延长一点,就可以避免这个问题。
n*2个端点加上起始点跟终点,两两之间不穿过任何线段的为可行路径建图。
最后以(0,0)开始dfs,看能否到达无穷远点。
#include<iostream>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
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 maxn = 100 + 5;
int n,V;
const int maxv = 200 + 5;
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()
{
// 构图
int i,j;
vector<Point> vertices;
vertices.push_back(Point(0, 0)); // 起点
vertices.push_back(Point(1e5, 1e5)); // 终点
for(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(i = 0; i < V; i++)
for(j = i+1; j < V; j++)
if(!IntersectWithAnySegment(vertices[i], vertices[j]))
G[i][j] = G[j][i] = 1;
return dfs(0);
} int main()
{
double x1, y1, x2, y2;
int i;Vector v;
while(scanf("%d",&n),n)
{
for(i = 0; i < n; i++)
{
scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
Point a = Point(x1, y1);
Point b = Point(x2, y2);
v = b - a;
v = v / Length(v);
p1[i] = a - v * 1e-6;
p2[i] = b + v * 1e-6;
}
if(find_path()) printf("no\n");
else printf("yes\n");
}
return 0;
}
LA 2797 平面区域dfs的更多相关文章
- LA 2797 (平面直线图PLSG) Monster Trap
题意: 平面上有n条线段,一次给出这n条线段的两个端点的坐标.问怪兽能否从坐标原点逃到无穷远处.(两直线最多有一个交点,且没有三线共交点的情况) 分析: 首先说明一下线段的规范相交:就是交点唯一而且在 ...
- 最大黑区域-DFS
最大黑区域 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...
- LA 7272 Promotions(dfs)
https://vjudge.net/problem/UVALive-7272 题意: 公司要提拔人,现在有n个人,现在有m条有向边,A->B表示A的表现比B好,也就是如果B晋升了,那么A肯定会 ...
- LA 3790 Overlapping Squares DFS
题意: 给出一个字符矩阵,问能否是不超过6个2×2的正方形组成的. 分析: 每次找一个最表面的正方形然后DFS就好了. 一个正方形被移开后,用一个特殊符号标记上,下次再匹配的时候就直接忽略这些位置. ...
- LA 6450 social advertising(dfs剪枝)
6450 Social AdvertisingYou have decided to start up a new social networking company. Other existing ...
- Surrounded Regions 包围区域——dfs
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
- LA 3263 平面划分
Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...
- LA 2797
题目链接 题意:训练指南283页: #include <iostream> #include <cstdio> #include <cstring> #includ ...
- 深度估计&平面检测小结
https://yq.aliyun.com/ziliao/582885 最近一段时间已知忙着赶图像分析与理解的项目,在三个星期内强行接触了CNN,MRF,Caffe,openCV在内的很多东西.现在项 ...
随机推荐
- Mac终端给命令设置别名alias的办法
在Mac里使用curl https://www.google.com,运行后得不到期望看到的google首页的HTML source code. vi ~/.bashrc, 输入下面两行内容. 以后每 ...
- k8s 如何 Failover?
上一节我们有 3 个 nginx 副本分别运行在 k8s-node1 和 k8s-node2 上.现在模拟 k8s-node2 故障,关闭该节点. 等待一段时间,Kubernetes 会检查到 k8s ...
- ArcMap所有Command GUID
The information in this topic is useful if you're trying to programmatically find a built-in command ...
- java后台验证码的生成
前台代码: <tr> <td>验证码</td> <td><input name="checkCode" type=" ...
- Python-DB接口规范
threadsafety 线程安全级别.threadsafety 这是一个整数, 取值范围如下: 0:不支持线程安全, 多个线程不能共享此模块 1:初级线程安全支持: 线程可以共享模块, 但不能共享连 ...
- javaEE(3)_servlet基础
一.Servlet简介 1.Servlet是sun公司提供的一门用于开发动态web资源的技术,Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序 ...
- ios runloop学习
今天突然才之间才意识到NSTimer这样的运行方式,是在多线程中实现的循环还是在主线程中去实现的呢.当然不可能是在主线程中的while那么简单,那样什么都干不了,简单看了下NSTimer是以同步方式运 ...
- PLAYGROUND 可视化
PLAYGROUND 可视化 由 王巍 (@ONEVCAT) 发布于 2015/09/23 在程序界,很多小伙伴都会对研究排序算法情有独钟,并且试图将排序执行的过程可视化,以便让大家更清晰直观地了解算 ...
- (60)zabbix网络发现介绍Network Discovery
网络发现简介 网络发现有什么用?网络发现怎么配置? 我们带着这两个问题开始我们的网络发现之旅. 比如小明有100台服务器,不想一台台主机去添加,能不能让zabbix自动添加主机呢,当然可以,网络发现便 ...
- 入门人工智能的首选语言为什么会是Python?
为何人工智能(AI)首选Python?当你读完这篇文章就会明白了.为何人工智能(AI)首选Python?读完这篇文章你就知道了.我们看谷歌的TensorFlow基本上所有的代码都是C++和Python ...