poj-1066 Treasure Hunt

75 篇文章 0 订阅
30 篇文章 0 订阅

题意:

有一个100大小的矩形房间,房间内有n堵墙;

每一堵墙都是横跨房间的,并且没有三个墙交于一点;

房间中的某个点有宝物,问从房间外面打破几道墙能到达宝物的地方;


题解:

思路十分神奇的一道题;

首先比较直观的思路就是建立对偶图跑最短路;

但是这我也不会写啊= =;

考虑到这些墙有一些特殊性——横跨房间;

就有了一种神奇的算法:

枚举房间外层的点,判断对于每一堵墙起点和终点是否在两边;

如果在两边,那么图中一定经过这堵墙!

思想是这样的,然后就是模拟水题了;

判断两边上叉积的乘积<0咯;


代码:


#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 33
using namespace std;
const double EPS=1e-8;
const double INF=1e100;
struct Point
{
	double x,y;
	Point(){}
	Point(double _,double __):x(_),y(__){}
	friend Point operator +(Point a,Point b)
	{
		return Point(a.x+b.x,a.y+b.y);
	}
	friend Point operator -(Point a,Point b)
	{
		return Point(a.x-b.x,a.y-b.y);
	}
	friend double operator *(Point a,Point b)
	{
		return a.x*b.x+a.y*b.y;
	}
	friend double operator ^(Point a,Point b)
	{
		return a.x*b.y-a.y*b.x;
	}
}T;
int top[4];
double st[4][N];
struct Line
{
	Point p,v;
	void read()
	{
		static Point temp;
		scanf("%lf%lf%lf%lf",&p.x,&p.y,&temp.x,&temp.y);
		if(p.x==0)
			st[0][++top[0]]=p.y;
		if(p.x==100)
			st[1][++top[1]]=p.y;
		if(p.y==0)
			st[2][++top[2]]=p.x;
		if(p.y==100)
			st[3][++top[3]]=p.x;
		if(temp.x==0)
			st[0][++top[0]]=temp.y;
		if(temp.x==100)
			st[1][++top[1]]=temp.y;
		if(temp.y==0)
			st[2][++top[2]]=temp.x;
		if(temp.y==100)
			st[3][++top[3]]=temp.x;
		v=temp-p;
	}
	
}l[N];
int main()
{
	int n,m,i,j,k,x,y,ans,cnt;
	double p;
	scanf("%d",&n);
	for(i=1;i<=n;i++)
		l[i].read();
	scanf("%lf%lf",&T.x,&T.y);
	for(i=0,ans=0x3f3f3f3f;i<4;i++)
	{
		st[i][++top[i]]=100;
		sort(st[i],st[i]+top[i]+1);
		for(j=1;j<=top[i];j++)
		{
			if(fabs(st[i][j]-st[i][j-1])<EPS)	continue;
			p=(st[i][j]+st[i][j-1])/2;
			Point t;
			switch(i)
			{
				case 0:t=Point(0,p);	break;
				case 1:t=Point(100,p);	break;
				case 2:t=Point(p,0);	break;
				case 3:t=Point(p,100);	break;
			}
			for(k=1,cnt=0;k<=n&&cnt<ans;k++)
			{
				if((l[k].v^t-l[k].p)*(l[k].v^T-l[k].p)<-EPS)
					cnt++;
			}
			ans=min(ans,cnt);
		}
	}
	printf("Number of doors = %d\n",ans+1);
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值