POJ1066Treasure Hunt
题意:
在一个正方形中,有n条线段(端点都在正方形的边上)将正方形分隔开。有一个点,想要从正方形外到这个点,只能走线段的中点。问最少需要经过多少个中点。
思路:
比较有意思的一道题。
朴素的可以想到,它虽然说是走中点,但是你走出的其实是一个“封闭区域”,你只要找从外面到目标点最少经过几块“封闭区域”。而我们又可以发现,你走出一块“封闭区域”,走中点或者边上其他的点的效果是相同的。那么我们就可讲边上的点与目标点连线,然后判断有条线和它相交。
注意n=0时候特判一下。
代码:
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<ctype.h>
#include<queue>
#include<set>
#include<stack>
#include<cmath>
#include<map>
#define pii pair<int,int>
#define ll long long
#define cl(x,y) memset(x,y,sizeof(x))
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define lson x<<1,l,mid
#define rson x<<1|1,mid+1,r
#define INF 1e18
const int N=1e6+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const double eps=1e-8;
const double pi=acos(-1);
using namespace std;
int sgn(double x)
{
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
else return 1;
}
struct point
{
double x,y;
point(){}
point(double xx,double yy){x=xx;y=yy;}
point operator +(point b){return point(x+b.x,y+b.y);}
point operator -(point b){return point(x-b.x,y-b.y);}
double operator ^(point b){return x*b.y-y*b.x;}//叉乘
double operator *(point b){return x*b.x+y*b.y;}//点乘
point operator *(double b){return point(x*b,y*b);}//数乘
void read(){scanf("%lf%lf",&x,&y);}
}p;
struct line
{
point s,e,l;
line(){}
line(point ss,point ee){s = ss;e = ee;l=e-s;}
void read(){s.read(),e.read();l=e-s;}
}l[50];
bool judge(line l1,line l2)
{
point a=l1.s,b=l1.e,c=l2.s,d=l2.e;
l2.l=l2.e-l2.s;
double x1=((c-a)^l1.l)*((d-a)^l1.l);
double x2=((a-c)^l2.l)*((b-c)^l2.l);
if(x1==0 || x2==0)
return sgn((a-c)*(b-c))<=0 || sgn((a-d)*(b-d))<=0 || sgn((c-a)*(d-a))<=0 || sgn((c-b)*(d-b))<=0;
return x1<0&&x2<0;
}
int ans=inf,n;
void solve(line k)
{
int sum=0,i;
for(i=1;i<=n;i++)
if(judge(l[i],k))
sum++;
ans=min(ans,sum);
return;
}
int main()
{
int i;
scanf("%d",&n);
for(i=1;i<=n;i++)
l[i].read();
p.read();
for(i=1;i<=n;i++)
{
solve(line{p,l[i].s});
solve(line{p,l[i].e});
}
if(n==0)
ans=1;
printf("Number of doors = %d",ans);
return 0;
}