POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066
Time Limit: 1000MS Memory Limit: 10000K
Description
An example is shown below:

Input
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
题意:
一个正方形底的金字塔,坐标为(0,0)->(100,100),里面情况类似于上图,有许多直接连接在最外层正方形上的墙,把整个金字塔底部分割成许多小房间;
现在专家们已经确定,其中某一个房间为宝藏房,并且得到了一个位于该房间内的点坐标,记为点p;
现在专家们要从最外面进行爆破开门法,一直炸到宝藏房,求最少需要开多少扇门。
题解:
枚举(0,0)-(0,100)-(100,100)-(100,0)这个正方形上的所有点(其实就是所有墙的端点),记为点q;
连接p与q两点得到一条线段,再去枚举所有的墙,通过判断是否规范相交确定一路上要经过多少堵墙,记为cnt;
顺便把(0,0)、(0,100)、(100,100)、(100,0)这四个点也按上面的办法去算一下cnt;
取所有cnt中最小的,加上1(外墙上还要开扇门),即为答案;
AC代码:
#include<cstdio>
#include<cmath>
#include<iostream>
using namespace std; const double eps = 1e-; struct Point{
double x,y;
Point(double tx=,double ty=):x(tx),y(ty){}
};
typedef Point Vctor; //向量的加减乘除
Vctor operator + (Vctor A,Vctor B){return Vctor(A.x+B.x,A.y+B.y);}
Vctor operator - (Point A,Point B){return Vctor(A.x-B.x,A.y-B.y);}
Vctor operator * (Vctor A,double p){return Vctor(A.x*p,A.y*p);}
Vctor operator / (Vctor A,double p){return Vctor(A.x/p,A.y/p);} int dcmp(double x)
{
if(fabs(x)<eps) return ;
else return (x<)?(-):();
}
bool operator == (Point A,Point B){return dcmp(A.x-B.x)== && dcmp(A.y-B.y)==;} double Cross(Vctor A,Vctor B){return A.x*B.y-A.y*B.x;} //判断线段是否规范相交
bool SegmentProperIntersection(Point a1,Point a2,Point b1,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)<;
} int n;
struct Seg{
Point a,b;
}wall[];
Point p;
int test(const Point& q)
{
int cnt=;
for(int i=;i<=n;i++)
if(SegmentProperIntersection(p,q,wall[i].a,wall[i].b)) cnt++;
return cnt;
}
int main()
{
cin>>n;
for(int i=;i<=n;i++) scanf("%lf%lf%lf%lf",&wall[i].a.x,&wall[i].a.y,&wall[i].b.x,&wall[i].b.y);
cin>>p.x>>p.y; int ans=0x3f3f3f3f;
for(int i=,tmp;i<=n;i++)
{
ans=min(test(wall[i].a),ans);
ans=min(test(wall[i].b),ans);
}
ans=min(test(Point(,)),ans);
ans=min(test(Point(,)),ans);
ans=min(test(Point(,)),ans);
ans=min(test(Point(,)),ans); cout<<"Number of doors = "<<ans+<<endl;
}
POJ 1066 - Treasure Hunt - [枚举+判断线段相交]的更多相关文章
- POJ 1066 Treasure Hunt --几何,线段相交
题意: 正方形的房子,给一些墙,墙在区域内是封闭的,给你人的坐标,每穿过一道墙需要一把钥匙,问走出正方形需要多少把钥匙. 解法: 因为墙是封闭的,所以绕路也不会减少通过的墙的个数,还不如不绕路走直线, ...
- POJ 2653 - Pick-up sticks - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=2653 Time Limit: 3000MS Memory Limit: 65536K Description Stan has n s ...
- 简单几何(线段相交) POJ 1066 Treasure Hunt
题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...
- POJ 2653 Pick-up sticks(判断线段相交)
Pick-up sticks Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 7699 Accepted: 2843 De ...
- poj 1066 Treasure Hunt (Geometry + BFS)
1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...
- POJ 2826 An Easy Problem? 判断线段相交
POJ 2826 An Easy Problem?! -- 思路来自kuangbin博客 下面三种情况比较特殊,特别是第三种 G++怎么交都是WA,同样的代码C++A了 #include <io ...
- 【POJ 2653】Pick-up sticks 判断线段相交
一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
随机推荐
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- SpringMVC由浅入深day02_7上传图片
7 上传图片 7.1 需求 在修改商品页面,添加上传商品图片功能. 7.2 springmvc中对多部件类型解析 在页面form中提交enctype="multipart/form-data ...
- popupWindow 在指定位置上的显示
先看效果图,免得浪费大家时间,看是不是想要的效果 . 直接上代码 ,核心方法. private void showPopupWindow(View parent) { if (popupWindow ...
- win10找回Windows照片查看器
- PHP 中文字符串截取
$str = "abcdef啊啊吧啊"; function my_sub($str, $st ,$len){ $ret = ""; for( $st; $len ...
- 【gitlab】创建ssh 秘钥
1).首先打开linux服务器,输入命令:ls -al ~/.ssh,检查是否显示有id_rsa.pub或者id_dsa.pub存在,如果存在请直接跳至第3步. 2).在bash中输入,注意这个地方的 ...
- PKI技术原理
转:http://3layer.blog.51cto.com/57448/20430 对称加密 symmetric cryptographic 非对称加密 asymmetric ...
- Android studio 插件安装
安装插件步骤 一 CodeGlance 最大的用途:可用于快速定位代码.显示在右侧 二 Android Studio Prettify 可以将代码中的字符串写在string.xml文件中 选中字符串鼠 ...
- java (10) 集合类
1.集合概述 集合按照存储结构可以分为两类,即单列集合 Collection 和双列集合 Map. * Collection 用于存储一系列符合某种规则的元素,它有两个重要的自接口,分别是List和S ...
- OGG日常运维监控的自动化脚本模板
#!/usr/bin/ksh export ORACLE_BASE=/oracle/ export ORACLE_SID=epmln1 export ORACLE_HOSTNAME=pmlnpdb1 ...