POJ 1066 Treasure Hunt(计算几何)
题意:给出一个100*100的正方形区域,通过若干连接区域边界的线段将正方形区域分割为多个不规则多边形小区域,然后给出宝藏位置,要求从区域外部开辟到宝藏所在位置的一条路径,使得开辟路径所需要打通的墙壁数最少("打通一堵墙"即在墙壁所在线段中间位置开一空间以连通外界),输出应打通墙壁的个数(包括边界上墙壁)。
题解:枚举每一个入口,在所有的情况中取穿墙数最少的输出即可,枚举每一个入口的时候,并不用枚举每条边的中间点,直接枚举该线段的两个顶点就行(因为要经过一个墙,那么从线段的任意地方进去都行,不必要每次从线段的中点过去),将枚举的顶点与终点(即宝藏所在位置)连成线段,然后就是判断剩下的线段与该线段相交的问题了(注意这里是严格相交),最后得出的数字加1即为结果(因为还有边界墙),还有就是需要特别处理n=0时的情况。
个人感觉,计算几何简单的题就是模版的事,只要记住这几个常用模板(叉积,线段相交,Point,Line)一般的计算几何就没问题了。
AC代码:
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int INF=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
#define prN printf("\n")
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define SIII(N,M,K) scanf("%d%d%d",&(N),&(M),&(K))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
const int MAX_N= 1000+5 ;
const double eps= 1e-9 ; int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x;
y = _y;
}
Point operator -(const Point &b)const
{
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const
{
return x*b.y - y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
};
struct Line
{
Point s,e;
Line() {}
Line(Point _s,Point _e)
{
s = _s;
e = _e;
} }; bool inter(Line l1,Line l2)
{
//这要多判断一步,是否是边界点,因为遍历就是从边界开始的,所以要除掉2个端点
if (fabs(l1.s.x-l2.s.x)<eps&&fabs(l1.s.y-l2.s.y)<eps||fabs(l1.s.x-l2.e.x)<eps&&fabs(l1.s.y-l2.e.y)<eps)
return false;
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= 0 &&
sgn((l1.s-l2.s)^(l2.e-l2.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= 0;
}
double dist(Point a,Point b)
{
return sqrt((b-a)*(b-a));
} Line line[35];
Point po[65];
int n,num,res=INF;
double endx,endy; void sol()
{
if (n==0)
{
puts("Number of doors = 1");
return;
}
rep(i,num)
{
int ans=0;
Line tel(po[i],Point(endx,endy));
rep(j,n)
{
if (inter(tel,line[j]))
{
ans++;
}
}
res=min(ans,res);
}
printf("Number of doors = %d\n",res+1);
} int main()
{
double x1,y1,x2,y2;
SI(n); rep(i,n)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
po[num++]=Point(x1,y1);
po[num++]=Point(x2,y2);
line[i]=Line(Point(x1,y1),Point(x2,y2));
}
scanf("%lf%lf",&endx,&endy);
sol();
return 0;
}
POJ 1066 Treasure Hunt(计算几何)的更多相关文章
- poj 1066 Treasure Hunt (Geometry + BFS)
1066 -- Treasure Hunt 题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方.可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁.问最少 ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 1066 Treasure Hunt(相交线段&&更改)
Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- poj 1066 Treasure Hunt
http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...
- POJ 1066 Treasure Hunt [想法题]
题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
- POJ 1066 Treasure Hunt【线段相交】
思路:枚举四边墙的门的中点,与终点连成一条线段,判断与其相交的线段的个数.最小的加一即为答案. 我是傻逼,一个数组越界调了两个小时. #include<stdio.h> #include& ...
- POJ 1066 Treasure Hunt --几何,线段相交
题意: 正方形的房子,给一些墙,墙在区域内是封闭的,给你人的坐标,每穿过一道墙需要一把钥匙,问走出正方形需要多少把钥匙. 解法: 因为墙是封闭的,所以绕路也不会减少通过的墙的个数,还不如不绕路走直线, ...
随机推荐
- php构造函数实例讲解
PHP官网定义: 复制代码 代码如下: 构造函数是类中的一个特殊函数,当使用 new 操作符创建一个类的实例时,构造函数将会自动调用.当函数与类同名时,这个函数将成为构造函数.如果一个类没有构造函数, ...
- spark新能优化之shuffle新能调优
shuffle调优参数 new SparkConf().set("spark.shuffle.consolidateFiles", "true") spark. ...
- DuiLib——第二篇UIBase
---------------------------------------------------------------------------------- 分析约定: private o-- ...
- python3安装Fabric模块
streamparse 项目的issuehttps://github.com/Parsely/streamparse/issues/172 fabric的一个支持Python3.4的forkhttps ...
- pip安装模块警告InsecurePlatformWarning: A true SSLContext object is not available.
在用pip7.1.2版本安装第三方模块时出现了如下警告: /usr/lib/python2.7/site-packages/pip-7.1.2-py2.7.egg/pip/_vendor/reques ...
- VGG-19 和 VGG-16 的 prototxt文件
VGG-19 和 VGG-16 的 prototxt文件 VGG-19 和 VGG-16 的 prototxt文件 VGG-16:prototxt 地址:https://gist.github.co ...
- Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code
Compute Mean Value of Train and Test Dataset of Caltech-256 dataset in matlab code clc;imPath = '/ho ...
- java多线程机制
多线程使用场景 1.同时需要做两件以上事情时需要开多个线程(例如:上传功能附带进度条显示,一边做上传,一边更新进度条状态.) 2.大量同类型数据需要进行处理(导入导出大量数据) 3.office文档转 ...
- log tree(merge)
http://www-users.cs.umn.edu/~he/diff/p256-severance.pdf http://www.eecs.harvard.edu/~margo/cs165/pap ...
- GlassFish Server is a compliant implementation of the Java EE 7 platform
1.9 GlassFish Server Tools GlassFish Server is a compliant implementation of the Java EE 7 platform. ...