计算几何——Treasure Hunt(线段相交)
题目链接http://poj.org/problem?id=1066
Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors.
An example is shown below:
Input
The 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
Print a single line listing the minimum number of doors which need to be created, in the format shown below.
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)的考古学家已经飞往埃及,检查关键行动的大金字塔。使用最先进的技术,他们能够确定金字塔的下部由一系列直线墙构成,这些墙相交形成许多封闭的密室。目前,没有门可以进入任何房间。这一最先进的技术也精确定位了藏宝室的位置。这些专注的(和贪婪的)考古学家想要做的是通过墙壁炸门,到达宝库。然而,为了尽量减少对中间房间的艺术品的损害(并留在他们的*赠款的炸药),他们希望爆炸通过最少的门数。出于结构完整性目的,门应仅在进入房间墙壁的中点爆炸。您将编写一个程序,确定此最小门数。
示例如下所示:
输入
输入将由一个案例组成。第一行将是一个整数 n (0 <= n <= 30) 指定内壁的数量,后跟包含每个墙的整数端点的 n 行 x1 y1 x2 y2 。金字塔的4个封闭墙的固定端点为(0,0);(0,100);(100,100) 和 (100,0), 不包括在墙壁列表中。内墙总是从一个外墙到另一个外墙,并安排,使不超过两个墙在任何时候都相交。您可能假定没有两个给定的墙壁重合。内墙上市后,宝物室将最后一条线包含宝藏的浮点坐标(保证不会躺在墙上)。
输出
打印一行,列出需要创建的最小门数,格式如下。
直接暴力枚举到目标的点的所有线,判断与几个线段相交,并且求出最小的相交数就行了,暴力出奇迹(QAQ)
#include <iostream>
#include<cmath>
#include<cstdio>
using namespace std;
const double eps=1e-6;
const int INF = 0x3f3f3f;
const int maxn=35;
typedef double db;
struct Point{
db x,y;
Point(db x=0,db y=0):x(x),y(y){}
};
typedef Point Vector;
Vector operator+(Vector a,Vector b){
return Vector(a.x+b.x,a.y+b.y);
}
Vector operator-(Vector a,Vector b){
return Vector(a.x-b.x,a.y-b.y);
}
Vector operator/(Vector a,db p){
return Vector(a.x/p,a.y/p);
}
Vector operator*(Vector a,db p){
return Vector(a.x*p,a.y*p);
}
db Cross(Vector a,Vector b){
return a.x*b.y-a.y*b.x;
}
db Dot(Vector a,Vector b){
return a.x*b.x+a.y*b.y;
}
int sgn(db x){
if(fabs(x)<eps) return 0;
if(x>0) return 1;
return -1;
}
bool OnSegment(Point p, Point a1, Point a2){
return sgn(Cross(a1-p, a2-p)) == 0 && sgn(Dot(a1-p, a2-p)) < 0;
}
//bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
// double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
// double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
// return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
//}
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
double c1 = Cross(a2-a1, b1-a1), c2 = Cross(a2-a1, b2-a1);
double c3 = Cross(b2-b1, a1-b1), c4 = Cross(b2-b1, a2-b1);
//if判断控制是否允许线段在端点处相交,根据需要添加
if(!sgn(c1) || !sgn(c2) || !sgn(c3) || !sgn(c4)){
bool f1 = OnSegment(b1, a1, a2);
bool f2 = OnSegment(b2, a1, a2);
bool f3 = OnSegment(a1, b1, b2);
bool f4 = OnSegment(a2, b1, b2);
bool f = (f1|f2|f3|f4);
return f;
}
return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}
struct Line{
Point p,v;
// Line(Point p,Point v):p(p),v(v){}
// Point point(db t){
// return p+(p-v)*t;
// }
}l[maxn];
int main(int argc, char** argv) {
int n;
db x,y;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%lf%lf%lf%lf",&l[i].p.x,&l[i].p.y,&l[i].v.x,&l[i].v.y);
//
// l[n+1].p.x=0,l[n+1].p.y=0;
// l[n+1].v.x=0,l[n+1].v.y=100;
//
// l[n+2].p.x=0,l[n+2].p.y=0;
// l[n+2].v.x=100,l[n+2].v.y=0;
//
// l[n+3].p.x=0,l[n+3].p.y=100;
// l[n+3].v.x=100,l[n+3].v.y=100;
//
// l[n+4].p.x=100,l[n+4].p.y=0;
// l[n+4].v.x=100,l[n+4].v.y=100;
Point p;
scanf("%lf%lf",&p.x,&p.y);
int minn=INF,ans;
Point a;
for(int i = 0;i<=100;i++){
a.x=i,a.y=0;
ans=0;
for(int j = 1;j<=n;++j){
if(SegmentProperIntersection(a,p,l[j].p,l[j].v)){
ans++;
}
}
minn=min(ans,minn);
}
for(int i = 0;i<=100;i++){
a.x=100,a.y=i;
ans=0;
for(int j = 1;j<=n;++j){
if(SegmentProperIntersection(a,p,l[j].p,l[j].v)){
ans++;
}
}
minn=min(ans,minn);
}
for(int i = 0;i<=100;i++){
a.x=i,a.y=100;
ans=0;
for(int j = 1;j<=n;++j){
if(SegmentProperIntersection(a,p,l[j].p,l[j].v)){
ans++;
}
}
minn=min(ans,minn);
}
for(int i = 0;i<=100;i++){
a.x=0,a.y=i;
ans=0;
for(int j = 1;j<=n;++j){
if(SegmentProperIntersection(a,p,l[j].p,l[j].v)){
ans++;
}
}
minn=min(ans,minn);
}
printf("Number of doors = %d\n",minn+1);
return 0;
}
上一篇: 二分图的性质及其应用(konig定理)
下一篇: 无向图的点·双联通分量