uvalive 2797 Monster Trap
题意:给定一些线段障碍,判断怪物能不能逃离到无穷远处。
思路:从(0,0)点能否到无穷远处。用BFS搜索。那满足什么样的点符合要求,能加入到图中呢?
遍历每个点,显然一开始已经在某些线段上的点要删去。再判断,两点之间的连线是否与其他线段有交。有则删去。
这道题要注意如果两条线段重合,怎么办?延长每条线段,如果交点在线段内,不算。
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<memory.h>
#include<cstdlib>
#include<vector>
#define clc(a,b) memset(a,b,sizeof(a))
#define LL long long int
#define up(i,x,y) for(i=x;i<=y;i++)
#define w(a) while(a)
const double inf=0x3f3f3f3f;
const int N = ;
const double PI = acos(-1.0);
using namespace std;
const double eps = 1e-; double dcmp(double x)
{
if(fabs(x) < eps) return ;
else return x < ? - : ;
} struct Point
{
double x, y;
Point(double x=, double y=):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)< && dcmp(c3)*dcmp(c4)<;
} bool OnSegment(const Point& p, const Point& a1, const Point& a2)
{
return dcmp(Cross(a1-p, a2-p)) == && dcmp(Dot(a1-p, a2-p)) < ;
} const int maxv = + ;
int V;
int G[maxv][maxv], vis[maxv]; bool dfs(int u)
{
if(u == ) return true; // 1是终点
vis[u] = ;
for(int v = ; v < V; v++)
if(G[u][v] && !vis[v] && dfs(v)) return true;
return false;
} const int maxn = + ;
int n;
Point p1[maxn], p2[maxn]; // 在任何一条线段的中间(在端点不算)
bool OnAnySegment(Point p)
{
for(int i = ; i < n; i++)
if(OnSegment(p, p1[i], p2[i])) return true;
return false;
} // 与任何一条线段规范相交
bool IntersectWithAnySegment(Point a, Point b)
{
for(int i = ; i < n; i++)
if(SegmentProperIntersection(a, b, p1[i], p2[i])) return true;
return false;
} bool find_path()
{
// 构图
vector<Point> vertices;
vertices.push_back(Point(, )); // 起点
vertices.push_back(Point(1e5, 1e5)); // 终点
for(int i = ; 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, , sizeof(G));
memset(vis, , sizeof(vis));
for(int i = ; i < V; i++)
for(int j = i+; j < V; j++)
if(!IntersectWithAnySegment(vertices[i], vertices[j]))
G[i][j] = G[j][i] = ;
return dfs();
} int main()
{
while(cin >> n && n)
{
for(int i = ; i < n; i++)
{
double x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
Point a = Point(x1, y1);
Point b = Point(x2, y2);
Vector v = b - a;
v = v / Length(v);
p1[i] = a + v * 1e-;
p2[i] = b + v * 1e-;
}
if(find_path()) cout << "no\n";
else cout << "yes\n";
}
return ;
}
uvalive 2797 Monster Trap的更多相关文章
- LA 2797 (平面直线图PLSG) Monster Trap
题意: 平面上有n条线段,一次给出这n条线段的两个端点的坐标.问怪兽能否从坐标原点逃到无穷远处.(两直线最多有一个交点,且没有三线共交点的情况) 分析: 首先说明一下线段的规范相交:就是交点唯一而且在 ...
- LA2797 Monster Trap
题意 PDF 分析 可以考虑建图,跑迷宫. 然后以线段端点,原点,和无穷大点建图,有边的条件是两点连线和墙没有交点. 但是对两个线段的交点处理就会有问题,所以把线段延长.另外还需要判断延长后在墙上,舍 ...
- [GodLove]Wine93 Tarining Round #10
比赛链接: http://www.bnuoj.com/v3/contest_show.php?cid=4159 题目来源: lrj训练指南---几何算法 Flag ID Title A Board ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
- linux shell trap的使用
原文地址:http://blog.sina.com.cn/s/blog_62eb16bb01014dbh.html 一. trap捕捉到信号之后,可以有三种反应方式: (1)执行一段程序来处理这一信号 ...
- Alignment trap 解决方法 【转 结合上一篇
前几天交叉编译crtmpserver到arm9下.编译通过,但是运行的时候,总是提示Alignment trap,但是并不影响程序的运行.这依然很令人不爽,因为不知道是什么原因引起的,这就像一颗定时炸 ...
- ARMLinux下Alignment trap的一些测试 【转自 李迟的专栏 CSDN http://blog.csdn.net/subfate/article/details/7847356
项目中有时会遇到字节对齐的问题,英文为“Alignment trap”,如果直译,意思为“对齐陷阱”,不过这个说法不太好理解,还是直接用英文来表达. ARM平台下一般是4字节对齐,可以参考文后的给出的 ...
随机推荐
- java.lang.NoSuchMethodError: javaxservlet.http.HttpServletRequest.isAsyncStarted()Z
鸣谢网址:http://stackoverflow.com/questions/25940571/java-lang-nosuchmethoderror-javaxservlet-http-https ...
- Ubuntu的挂起和休眠
Ubuntu的挂起和休眠 之前一直没关注过这方面的信息,因为以前只是在台式机上面用Ubuntu,笔记本一直都是Windows.随着Windows越来越傻冒,最近决定将常用系统转为Ubuntu,才注意到 ...
- (转)基于即时通信和LBS技术的位置感知服务(二):XMPP协议总结以及开源解决方案
在<基于即时通信和LBS技术的位置感知服务(一):提出问题及解决方案>一文中,提到尝试使用XMPP协议来实现即时通信.本文将对XMPP协议框架以及相关的C/S架构进行介绍,协议的底层实现不 ...
- uva 10105
数学 杨辉三角 多项式系数 #include <cstdio> int f[13] = {1}; void init() { for (int i = 1; i < 13; i+ ...
- 修改jmeter jvm参数
记录下常用的linux下 jmeter jvm参数修改,打开jmeter安装目录/bin/jmeter(非jmeter.sh) 1. 修改默认堆内存大小 #默认的 HEAP="-Xms512 ...
- PHP基础语法2
数组 PHP有两种数组:索引数组.关联数组. 函数 自定义函数 自定义函数通过将一组代码封装起来,使代码进行复用,程序结构与逻辑更加清晰 返回值 使用return关键字可以使函数返回值,可以返回包括数 ...
- thinkphp URL相关
具体详见tp文档. 此处仅做学习笔记. 后缀配置: // 模板文件后缀名 'TMPL_TEMPLATE_SUFFIX'=>'.html', // 伪静态文件后缀名 'URL_HTML_SUFFI ...
- Django处理文件上传File Uploads
HttpRequest.FILES 表单上传的文件对象存储在类字典对象request.FILES中,表单格式需为multipart/form-data <form enctype="m ...
- CSS那些事儿-阅读随笔3(清除浮动)
浮动主要是由浮动(float)属性导致的页面错位现象,清除浮动不仅能解决页面错位的现象,还可以解决子元素浮动导致父元素背景无法自适应子元素高度的问题.在CSS样式中,主要利用clear属性中的both ...
- CSS那些事儿-阅读随笔2(选择符的组合与优先级/权重)
在知道了CSS选择符最基础的知识后,就要综合利用它们了.这里就记录几种常见的用法. 1.针对性的使用类选择符或者ID选择符 类选择符在一个页面中可能会在不同的地方应用,那么就需要有针对性地使用类选择符 ...