Treasure Hunt POJ - 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.
题意:给定区域,并给出线段划分了改区域。问从外面出发,最小需要经过几座墙可以到达终点。
思路: 从边界的点与终点构成一个线段,记录经过与给的线段交点个数,输出最小个数。此处注意要判断四个顶点与终点构成的线段。
AC代码:
#include
#include
#include
#include
#include
using namespace std;
const int maxn=33;
const int inf=0x7f7f7f7f;
struct point
{
double x,y;
point(){}
point(double _x,double _y){x=_x;y=_y;}
};
struct egde
{
point start,end;//
egde(){}
egde(point a,point b){
start=a;end=b;}
}edges[maxn];
double multi(point p1,point p2,point p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool intAcross(egde v1,egde v2)
{
if(max(v1.start.x,v1.end.x)>=min(v2.start.x,v2.end.x)&&
max(v2.start.x,v2.end.x)>=min(v1.start.x,v1.end.x)&&
max(v1.start.y,v1.end.y)>=min(v2.start.y,v2.end.y)&&
multi(v2.start,v1.end,v1.start)*multi(v1.end,v2.end,v1.start)>0&&
multi(v1.start,v2.end,v2.start)*multi(v2.end,v1.end,v2.start)>0) return 1;//相交
return 0;
}
int main()
{
int n;
double a,b,c,d;
point boss;
scanf("%d",&n);
for(int i=0;iscanf("%lf%lf%lf%lf",&a,&b,&c,&d);
edges[i]=egde(point(a,b),point(c,d));
}
scanf("%lf%lf",&boss.x,&boss.y);
int ans=inf;
for(int i=0;iint cnt=0;
egde left=egde(boss,edges[i].start);
egde right=egde(boss,edges[i].end);
for(int j=0;jif(i!=j&&intAcross(left,edges[j])) cnt++;
//cout<<"left "<
ans=min(ans,cnt+1);
cnt=0;
for(int j=0;jif (i!=j&&intAcross(right,edges[j])) cnt++;
ans=min(ans,cnt+1);
}
egde dd[5];
dd[1]=egde(boss,point(0,0)); dd[2]=egde(boss,point(100,0));
dd[3]=egde(boss,point(100,100));dd[4]=egde(boss,point(0,100));
for(int i=1;i<5;i++)
{
int cnt=0;
for(int j=0;jif(intAcross(dd[i],edges[j])) cnt++;
ans=min(ans,cnt+1);
}
printf("Number of doors = %d \n",ans);
return 0;
}