您的位置:首页 > 其它

poj 1066 Treasure Hunt

2016-04-12 21:09 260 查看
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条线段,问你最小开几个门到达目的地)

思路:枚举四面的点到目的地的距离(不能选(0,0)(100,0)(0,100)(100,100),求最小的交点次数。

#include<iostream>
#include<cstdio>
using namespace std;
const double EXP=1e-5;
struct point
{
double x,y;
};
struct vector
{
point start,end;
};
point p;
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);
}
int Across(vector v1,vector 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 distance(point a,point b)
{
return (a.x-b.x)*(a.x-b.x)-(a.y-b.y)*(a.y*b.y);
}
vector line[100],l;
point s,e;
int main()
{
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
{
cin>>line[i].start.x>>line[i].start.y>>line[i].end.x>>line[i].end.y;
}
cin>>s.x>>s.y;
int minn=30000,c,j,i;
for(int i=1;i<100;i++)
{		j=0;
c=0;
l.start.x=s.x,l.start.y=s.y,l.end.x=i,l.end.y=j;
for(int z=0;z<n;z++)
{
if(Across(l,line[z]))
c++;
}
if(c<minn)
minn=c;
}
for(int i=1;i<100;i++)
{		j=100;
c=0;
l.start.x=s.x,l.start.y=s.y,l.end.x=i,l.end.y=j;
for(int z=0;z<n;z++)
{
if(Across(l,line[z]))
c++;
}
if(c<minn)
minn=c;
}
for(int j=1;j<100;j++)
{		i=100;
c=0;
l.start.x=s.x,l.start.y=s.y,l.end.x=i,l.end.y=j;
for(int z=0;z<n;z++)
{
if(Across(l,line[z]))
c++;
}
if(c<minn)
minn=c;
}
for(int j=1;j<100;j++)
{		i=0;
c=0;
l.start.x=s.x,l.start.y=s.y,l.end.x=i,l.end.y=j;
for(int z=0;z<n;z++)
{
if(Across(l,line[z]))
c++;
}
if(c<minn)
minn=c;
}
printf("Number of doors = %d\n",minn+1);
}
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: