poj1066 Treasure Hunt

Treasure Hunt
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 5579 Accepted: 2316

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 

题意:简单来说就是一个人被困在一个100*100的房间里,然后有N张门。每张门是一条线段,有两个端点,问这个人逃脱这个房间最少要打开几张门,最外围的墙壁也算一张(Ps:所以这里最后的结果要+1)。

题解:我是枚举最外围相邻两个点任意的的坐标,然后从这点笔直的到这个人所在的坐标做一条线段,求出与这条线段相交有多少条线就是结果了,取最小的即可。这里我用了一个机智的极角排序,我都被自己的瞎鸡巴的机智给电到了。PS:最后结果有点点投机取巧,这里就是最好还是取最边缘的点比较好。不过AC还是没问题的。嗯,就这样了。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
struct point 
{
	double x,y;
	point(){}
	point(double x,double y):x(x),y(y){}
};
struct line
{
	point a,b;
};
point p[200];
line l[100];
double cross(point a,point b,point c)
{
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}
double dis(point a,point b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int cmp(point a,point b)
{
	double k=cross(p[0],a,b);
	if (fabs(k)<=eps)
		if (a.y==0 && b.y==0)
		{
			if (dis(p[0],a)-dis(p[0],b)>eps)
				return 0;
		}
		else
		{
			if (dis(p[0],b)-dis(p[0],a)>eps)
				return 0;
		}
	if (k<0)
		return 0;
	return 1;
}                   //极角排序 机智的我! 
point newp(point a,point b)
{
	point c=point((a.x+b.x)/2,(a.y+b.y)/2);
	return c;
}
int pd(point a,point b,point c,point d)
{
	double k1=cross(a,b,c),k2=cross(a,b,d);
	double k3=cross(c,d,b),k4=cross(c,d,a);
	if (k1*k2<0 && k3*k4<0) return 1;
	return 0;
}
int main()
{
	int n,i,j;
	double x1,x2,y1,y2,x,y;
	while (~scanf("%d",&n))
	{
		memset(p,0,sizeof(p));
		memset(l,0,sizeof(l));
		int t=1;
		p[0].x=0;p[0].y=0;
		for (i=1;i<=n;i++)
		{
			scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
			p[t].x=x1;p[t++].y=y1;
			p[t].x=x2;p[t++].y=y2;
			l[i].a=p[t-2];
			l[i].b=p[t-1];
		}
		t--;
		sort(p+1,p+1+t,cmp);
		//cout<<cross(p[0],p[1],p[2])<<endl;
		/*for (i=0;i<=t;i++)
			cout<<p[i].x<<" "<<p[i].y<<endl;*/
		point o;
		scanf("%lf%lf",&o.x,&o.y);
		point pp=newp(p[1],p[t]);
		line ll;
		ll.a=pp;
		ll.b=o;
		int ans=0;
		for (i=1;i<=n;i++)
			if (pd(ll.a,ll.b,l[i].a,l[i].b))
				ans++;
		for (i=2;i<=t;i++)
		{
			pp=newp(p[i-1],p[i]);
			ll.a=pp;
			ll.b=o;
			int temp=0;
			for (j=1;j<=n;j++)
				if (pd(ll.a,ll.b,l[j].a,l[j].b))
					temp++;
			if (temp<ans)
				ans=temp;
		}
		//for (i=1;i<=ans;i++)
		printf("Number of doors = %d\n",ans+1);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值