题目大意:一个平面区域有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的更多相关文章

  1. LA 2797 (平面直线图PLSG) Monster Trap

    题意: 平面上有n条线段,一次给出这n条线段的两个端点的坐标.问怪兽能否从坐标原点逃到无穷远处.(两直线最多有一个交点,且没有三线共交点的情况) 分析: 首先说明一下线段的规范相交:就是交点唯一而且在 ...

  2. 最大黑区域-DFS

    最大黑区域 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  3. LA 7272 Promotions(dfs)

    https://vjudge.net/problem/UVALive-7272 题意: 公司要提拔人,现在有n个人,现在有m条有向边,A->B表示A的表现比B好,也就是如果B晋升了,那么A肯定会 ...

  4. LA 3790 Overlapping Squares DFS

    题意: 给出一个字符矩阵,问能否是不超过6个2×2的正方形组成的. 分析: 每次找一个最表面的正方形然后DFS就好了. 一个正方形被移开后,用一个特殊符号标记上,下次再匹配的时候就直接忽略这些位置. ...

  5. LA 6450 social advertising(dfs剪枝)

    6450 Social AdvertisingYou have decided to start up a new social networking company. Other existing ...

  6. Surrounded Regions 包围区域——dfs

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...

  7. LA 3263 平面划分

    Little Joey invented a scrabble machine that he called Euler, after the great mathematician. In his ...

  8. LA 2797

    题目链接 题意:训练指南283页: #include <iostream> #include <cstdio> #include <cstring> #includ ...

  9. 深度估计&平面检测小结

    https://yq.aliyun.com/ziliao/582885 最近一段时间已知忙着赶图像分析与理解的项目,在三个星期内强行接触了CNN,MRF,Caffe,openCV在内的很多东西.现在项 ...

随机推荐

  1. codevs 2618 核电站问题

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题目描述 Description 一个核电站有N个放核物质的坑,坑排列在一条直线上.如果连续M个坑中放入核物质,则会 ...

  2. rhythmbox插件开发笔记2:背景知识学习 D-Bus&VFS&Gio& Python GTK+ 3

    这次主要简单介绍下相关的背景知识 D-Bus&VFS&Gio& Python GTK+ 3  D-Bus D-Bus是开源的进程通信(IPC)系统,它允许多个进程进行实时通信. ...

  3. docker安装Tensorflow并使用jupyter notebook

    目前网上提供的大多数的方法都是如下: docker pull tensorflow/tensorflow docker run -it -p : tensorflow/tensorflow 但是按照步 ...

  4. jsc 解码窥探

    先使用 JS_DecodeScript反编译jsc  得到AST树 AST树词法解析 http://esprima.org/ AST还原成源码: npm install escodegen AST树遍 ...

  5. QT +自定义控件-spin+slider

    动手实现自定义控件: 1.首先在ui界面中添加一个(Widget)容器类.如图中的1所示 2.在项目中添加一个SmallWidget类,如下: 3.接着在程序编辑界面进行程序编辑如下: #includ ...

  6. python之dic {字典}(重要指数*****)

    1. 什么是字典 {'name': '汪峰', 'age': 18} '键':'值' 别的语言键值对数据 键: 必须是可哈希(不可变的数据类型),并且是唯一的 值: 任意 可以保存任意类型的数据 字典 ...

  7. linx vim 文件操作 ubuntu server 软件源

    mv /etc/danted.conf /etc/danted.conf.bak sudo wget https://files.cnblogs.com/files/marklove/danted.t ...

  8. Delphi 中内存映射对于大文件的使用

    这篇文章主要介绍了Delphi 中内存映射对于大文件的使用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下 Delphi 中内存映射对于大文件的使用 平时很少使用大文件的内存映射,碰巧遇到了 ...

  9. Bootstrap历练实例:警告样式按钮

    <!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...

  10. 计算机应用 office系列 常用术语英文

    首先,Excel 办公室系列软件——Office series Software 微软——Microsoftware 电子表格 Excel 第一行称为标题栏——title bar 第二行称为菜单栏—— ...