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在内的很多东西.现在项 ...
随机推荐
- python代理检测
import socket,threading,os,sys,queue,re socket.setdefaulttimeout(5) path=sys.path[0] if os.path.isfi ...
- HtmlUnit爬取Ajax动态生成的网页以及自动调用页面javascript函数
HtmlUnit官网的介绍: HtmlUnit是一款基于Java的没有图形界面的浏览器程序.它模仿HTML document并且提供API让开发人员像是在一个正常的浏览器上操作一样,获取网页内容,填充 ...
- Bootstrap历练实例:超小的按钮
<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content=& ...
- Mac 输入法小技巧
相信使用Mac的朋友第一次使用Mac首先要考虑的就是输入法的问题,现在越来越多的第三方输入法都开始支持Mac平台,是否有同学仍然执着于看似“不符”国人习惯用法的OS X自带拼音输入法呢?自带的拼音输入 ...
- [LUOGU] 1717 钓鱼
题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请他吃一次"年年大丰收",为了表示诚意,他还决定亲自去钓鱼,但是,因为 ...
- 简易的mysql性能查询脚本
#!/bin/bash mysqladmin -P3306 -uroot -p -h127. -r -i ext |\ awk -F"|" \ "BEGIN{ count ...
- perl学习之:subs函数
在Perl中,sub关键字主要是为了定义一个子例程,那么subs又是什么呢? 首先subs是一个函数,用于预先声明子例程,函数的参数是预声明的函数名列表.那么这个函数存在的意义是什么?首先,通过该函数 ...
- perl学习之裸字
use strict包含3个部分.其中之一(use strict "subs")负责禁止乱用的裸字. 这是什么意思呢? 如果没有这个限制,下面的代码也可以打印出"hell ...
- Django 千锋培训的学习笔记(2)
Django 千锋培训读书笔记 https://www.bilibili.com/video/av17879644/?p=1 切换到创建项目的目录 cd C:\Users\admin\Desktop\ ...
- 链式链表的C风格实现
头文件: #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef void LinkList; //将数据的类型分离,相当于句柄 //只是一个小节点 包含着 ...