Description
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
大致题意:一个正方形区域中有n个墙,每个墙可以看成一条线段,两个端点都在正方形的边界上,这些墙将该正方形区域划分成了很多的小空间,然后告诉你宝藏的地点,问你最少需要炸掉多少墙才能从外面走到宝藏地点。正方形四条边也是墙,需要炸掉。宝藏点保证不在墙上。
思路:枚举墙在边界上的点,然后连接宝藏点,记录下所交的线段数,即此种情况下需要炸掉的墙的数量,然后取最小值,最后还要加上1。
代码如下
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double INF=0x3f3f3f3f;
int dcmp(double x)
{
if(fabs(x)return 0;
return x<0?-1: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.x + y*b.y;
}
double operator ^(const Point &b)const {
return x*b.y - y*b.x;
}
int operator ==(const Point &b)const {
if(dcmp(x-b.x)==0&&dcmp(y-b.y)==0)//两点相同
return 0;
else return 1;
}
};
struct Line
{
Point a,b;
Line() {}
Line(Point _a,Point _b)
{
a=_a;
b=_b;
}
};
const int N=100;
Line line[N];
Point dian[N];
double xmult(Point p0,Point p1,Point p2)
{
return (p1-p0)^(p2-p0);
}
bool inter(Line L1,Line L2)//判断两条线段是否相交,相交返回1
{
return
max(L1.a.x,L1.b.x) >= min(L2.a.x,L2.b.x) &&
max(L2.a.x,L2.b.x) >= min(L1.a.x,L1.b.x) &&
max(L1.a.y,L1.b.y) >= min(L2.a.y,L2.b.y) &&
max(L2.a.y,L2.b.y) >= min(L1.a.y,L1.b.y) &&
dcmp((L2.a-L1.a)^(L1.b-L1.a))*dcmp((L2.b-L1.a)^(L1.b-L1.a)) <= 0 &&
dcmp((L1.a-L2.a)^(L2.b-L2.a))*dcmp((L1.b-L2.a)^(L2.b-L2.a)) <= 0;
}
int main()
{
int n;
double x,y,x1,y1,x2,y2;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[i]=Line(Point(x1,y1),Point(x2,y2));
dian[2*i-1]=Point(x1,y1);
dian[2*i]=Point(x2,y2);
}
scanf("%lf%lf",&x,&y);
int ans=1e5;
if(n==0)
ans=0;
else
{
for(int i=1;i<=2*n;i++)
{
int sum=0;
Line L=Line(Point(x,y),dian[i]);
for(int j=1;j<=n;j++)
{
if(dian[i]==line[j].a&&dian[i]==line[j].b)
if(inter(L,line[j]))
sum++;
}
ans=min(ans,sum);
}
}
printf("Number of doors = %d\n",ans+1);
return 0;
}