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在内的很多东西.现在项 ...
随机推荐
- 了解springcloud
spring cloud比较不错的文章 https://blog.csdn.net/zhaozhenzuo/article/details/52803490?utm_source=blogxgwz9 ...
- UVA 12549 Sentry Robots (最小点覆盖)
这道题挺像hdu 5093 Battle ships的,不过那道题是要求最多放置的点数,而这道题是要求最小点覆盖. 顶点覆盖的定义是:在G中任意边至少有一个端点属于顶点集合S. 一个重要的位置有(x, ...
- Codeforces Round #316 (Div. 2) B Simple Game 贪心
贪心,如果m分成的两个区间长度不相等,那么选长的那个区间最接近m的位置,否则选m-1位置,特判一下n等于1的情况 #include<bits/stdc++.h> using namespa ...
- 特别困的学生 UVa12108(模拟题)
一.题目 课堂上有n个学生(n<=10).每个学生都有一个“睡眠-清醒”周期,其中第i个学生醒Ai分钟后睡Bi分钟,然后重复(1<=Ai,Bi<=5),初始第i个同学处于他的周期的C ...
- 使用gcc -g编译,gdb调试时仍然存在“no debug symbols found”的错误
今天为调试一段代码,使用gcc将程序用-g选项重新编译.但是使用gdb进行debug时,仍然出现“no debug symbols found”的错误.仔细检查了一下Makefile,原来后面定义的连 ...
- Spring框架针对dao层的jdbcTemplate操作crud之query查询数据操作
查询目标是完成3个功能: (1)查询表,返回某一个值.例如查询表中记录的条数,返回一个int类型数据 (2)查询表,返回结果为某一个对象. (3)查询表,返回结果为某一个泛型的list集合. 一.查询 ...
- NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY
NSLayoutConstraint.constraintsWithVisualFormat详解,以及AlignAllCenterY 转载2015-07-08 18:02:02 鉴于苹果官方文档的解释 ...
- webpack4.x加vue模板文件简单还原vue-cli
1.首先 npm init -y 创建一个项目 2.安装vue npm install vue --save 3.然后安装webpack 注意如果全局没有还要安装全局的webpack和webpack- ...
- Java--泛型理解和使用 (List<String> list = new ArrayList<String>(); )
List<String> list = new ArrayList<String>(); 第一次看到这行代码是一头雾水,查了好久才弄清楚这是什么东西,怎么用,所以记录下来,方便 ...
- 初涉平衡树「treap」
treap:一种平衡的二叉搜索树 什么是treap(带旋) treap=tree+heap,这大家都知道.因为二叉搜索树(BST)非常容易被卡成一条链而影响效率,所以我们需要一种更加平衡的树形结构,从 ...