POJ_1066_Treasure Hunt_判断线段相交
POJ_1066_Treasure Hunt_判断线段相交
Description
An example is shown below:

Input
input will consist of one case. The first line will be an integer n (0
<= n <= 30) specifying number of interior walls, followed by n
lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4
enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100);
(100,100) and (100,0) and are not included in the list of walls. The
interior walls always span from one exterior wall to another exterior
wall and are arranged such that no more than two walls intersect at any
point. You may assume that no two given walls coincide. After the
listing of the interior walls there will be one final line containing
the floating point coordinates of the treasure in the treasure room
(guaranteed not to lie on a wall).
Output
Sample Input
7
20 0 37 100
40 0 76 100
85 0 0 75
100 90 0 90
0 71 100 61
0 14 100 38
100 47 47 100
54.5 55.4
Sample Output
Number of doors = 2
来自古文物和古玩博物馆(ACM)的考古学家已经飞到埃及去研究大金字塔的大金字塔。
利用最先进的技术,他们能够确定金字塔的底层是由一系列的直线墙构成的,这些墙与许多封闭的房间相交。目前,没有任何门可以进入任何一个房间。
这种最先进的技术也精确定位了宝藏室的位置。
这些专门的(和贪婪的)考古学家想要做的是把门从墙上炸开,到达宝藏室。然而,为了尽量减少在中间的房间里对艺术作品的损害(并在政府的资助下,他们想要通过最少的门数来进行爆炸)。
出于结构完整性的目的,门应该只在进入房间的墙壁的中点被炸开。你要写一个程序来确定这个最小的门数。 注意是进入房间的墙壁的中点而不是直线墙的中点。
那样这道题就变得非常简单了,直接枚举每个点判断这个点与宝藏的连线经过多少条线段即可。
然后+1就是答案。 代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std;
typedef double f2;
struct Point {
f2 x,y;
Point() {}
Point(f2 x_,f2 y_) :
x(x_),y(y_) {}
Point operator + (const Point &p) const {return Point(x+p.x,y+p.y);}
Point operator - (const Point &p) const {return Point(x-p.x,y-p.y);}
Point operator * (f2 rate) const {return Point(x*rate,y*rate);}
};
struct Line {
Point p,v;
Line() {}
Line(const Point &p_,const Point &v_) :
p(p_),v(v_) {}
};
f2 cross(const Point &p1,const Point &p2) {return p1.x*p2.y-p1.y*p2.x;}
f2 turn(const Point &p1,const Point &p2,const Point &p3) {
return cross(p3-p1,p2-p1);
}
bool judge(const Line &l1,const Line &l2) {
if(turn(l1.p,l1.v,l2.p)*turn(l1.p,l1.v,l2.v)>=0) return 0;
if(turn(l2.p,l2.v,l1.p)*turn(l2.p,l2.v,l1.v)>=0) return 0;
return 1;
}
Line a[50];
Point T;
int n;
int solve(const Line &l) {
int i,re=0;
for(i=1;i<=n;i++) {
if(judge(a[i],l)) re++;
}
return re;
}
int main() {
scanf("%d",&n);
if(!n) {
printf("Number of doors = %d",1); return 0;
}
int i;
int ans=1<<30;
for(i=1;i<=n;i++) {
scanf("%lf%lf%lf%lf",&a[i].p.x,&a[i].p.y,&a[i].v.x,&a[i].v.y);
}
scanf("%lf%lf",&T.x,&T.y);
for(i=1;i<=n;i++) {
Line l=Line(T,a[i].p);
ans=min(ans,solve(l));
l=Line(T,a[i].v);
ans=min(ans,solve(l));
}
printf("Number of doors = %d",ans+1);
}
POJ_1066_Treasure Hunt_判断线段相交的更多相关文章
- 还记得高中的向量吗?leetcode 335. Self Crossing(判断线段相交)
传统解法 题目来自 leetcode 335. Self Crossing. 题意非常简单,有一个点,一开始位于 (0, 0) 位置,然后有规律地往上,左,下,右方向移动一定的距离,判断是否会相交(s ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- 判断线段相交(hdu1558 Segment set 线段相交+并查集)
先说一下题目大意:给定一些线段,这些线段顺序编号,这时候如果两条线段相交,则把他们加入到一个集合中,问给定一个线段序号,求在此集合中有多少条线段. 这个题的难度在于怎么判断线段相交,判断玩相交之后就是 ...
- hdu 1086(判断线段相交)
传送门:You can Solve a Geometry Problem too 题意:给n条线段,判断相交的点数. 分析:判断线段相交模板题,快速排斥实验原理就是每条线段代表的向量和该线段的一个端点 ...
- POJ_2653_Pick-up sticks_判断线段相交
POJ_2653_Pick-up sticks_判断线段相交 Description Stan has n sticks of various length. He throws them one a ...
- POJ_1556_The Doors_判断线段相交+最短路
POJ_1556_The Doors_判断线段相交+最短路 Description You are to find the length of the shortest path through a ...
- POJ 1066--Treasure Hunt(判断线段相交)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7857 Accepted: 3247 Des ...
- POJ2653 Pick-up sticks 判断线段相交
POJ2653 判断线段相交的方法 先判断直线是否相交 再判断点是否在线段上 复杂度是常数的 题目保证最后答案小于1000 故从后往前尝试用后面的线段 "压"前面的线段 排除不可能 ...
随机推荐
- 到底创建了几个String对象?
到底创建了几个String对象? 标签: 堆栈使用 对象创建 分类: 开发技术 关键字: java 面试题 string 创建几个对象 作者:臧圩人(zangweiren) 网址:http://zan ...
- Django之Apps源码学习
先了解下官方文档的介绍 Django包含了一个已经安装应用的注册表,这个注册表存储着配置信息以及用来自省,同时也维护着可用模型的列表. 这个注册表就是apps,位于django.apps下,本质上是一 ...
- Day12 CSS简单用法
当我想要将html中的部分属性修改的时候,如果单个改的话,费时费力,这时候我就需要利用css和html结合起来了. <head> <meta charset="UTF-8& ...
- MLDS笔记:Generalization
1 泛化能力 用VC维来衡量一个模型的表达能力,比如2维线性模型的VC维为3. 在图1-2中,随便给啥训练数据该model都能learn起来. 从理论上来看,当2个model在训练数据上表现一样时,为 ...
- 今年暑假不AC - HZNU寒假集训
今年暑假不AC "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" "@#$%^&a ...
- jvm内存结构(一)
学习之余,整理了下JVM的资料 堆: 需要重点关注的一块区域,涉及到内存的分配与回收 方法区: 用于存储已经被虚拟机加载的类信息.常量.静态变量等数据,也叫永久区 常量池: 用于存放编译期生成的各种字 ...
- Java 锁机制 synchronized
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/75126630 本文出自[赵彦军的博客] 1.前言 在多线程并发编程中Synchro ...
- sudo pip install MySQLdb
安装数据库第三方包,报错: Could not find a version that satisfies the requirement MySQLdb (from versions: )No ma ...
- unity零基础开始学习做游戏(六)背景给我“滚”~
-------小基原创,转载请给我一个面子 一望无际的...空旷场景,看着实在是难受,不如添加些背景吧.如果要真的想好好设计关卡背景的话,最好是做一个地图编辑器,不过做开发工具毕竟有点点复杂且枯燥,以 ...
- java(二、基础语法和基本数据类型)
Java 基础语法 一个Java程序可以认为是一系列对象的集合,而这些对象通过调用彼此的方法来协同工作.下面简要介绍下类.对象.方法和实例变量的概念. 对象:对象是类的一个实例,有状态和行为.例如,一 ...