POJ 1066 Treasure Hunt(思维+线段相交判断)
思路:枚举每一个端点,与终点形成线段,与其它线段进行比较,如果有相交,说明要多炸一道墙。同时要将四个角落也加进端点中。这道题比较考思维,但是一般都是要枚举端点的,这一点可以列入这些题型的考虑中。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include <cstdio>
using namespace std;
#define ll long long
int n;
struct Point
{
double x,y;
Point(double x= 0, double y = 0):x(x),y(y){}
}p[1000];
typedef Point Vector;
const int INF = 0x3f3f;
Vector operator-(Point A,Point B)
{
return Vector(A.x-B.x,A.y-B.y);
}
Vector operator *(Vector A, double p)
{
return Vector(A.x*p, A.y*p);
}
Vector operator +(Vector A, Vector B){
return Vector(A.x+B.x, A.y+B.y);
}
Vector operator /(Vector A, double p){return Vector(A.x/p, A.y/p);}
double Dot(Vector A, Vector B){ return A.x*B.x + A.y*B.y;}
double Length(Vector A){ return sqrt(Dot(A, A));}
double Cross(Vector A, Vector B){return A.x*B.y-A.y*B.x;}
double dis(Point a,Point b)
{
return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
const double eps = 1e-6;
int sgn(double x){
if(fabs(x) < eps)
return 0;
if(x < 0)
return -1;
return 1;
}
struct Line{
Point v, p;
Line(){}
Line(Point v, Point p):v(v), p(p){}
Point point(double t){
return v+(p-v)*t;//·µ»ØµãP = v + (p - v)*t
}
}line[105000];
bool SegmentProperIntersection(Point a1, Point a2, Point b1, Point b2){
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return (sgn(c1)*sgn(c2) < 0 && sgn(c3)*sgn(c4) < 0);
}
queue<int>q;
queue<int>temp;
int Count(Point a, Point b)
{
int cnt = 1;
for(int i = 1;i<=n;i++)
{
if(SegmentProperIntersection(a,b,line[i].v, line[i].p))
cnt++;
}
return cnt;
}
int main()
{
while(scanf("%d",&n) !=EOF)
{
int cnt = 0;
p[++cnt] = Point(0, 0);
p[++cnt] = Point(0, 100);
p[++cnt] = Point(100, 0);
p[++cnt] = Point(100, 100);
for(int i = 1;i<=n;i++)
{
Point a, b;
scanf("%lf%lf%lf%lf", &a.x, &a.y, &b.x, &b.y);
line[i] = Line(a,b);
p[++cnt] = a;
p[++cnt] = b;
}
Point ed;
scanf("%lf%lf",&ed.x, &ed.y);
int minn = 0x3f;
for(int i = 1;i<=cnt;i++)
{
minn = min(Count(p[i],ed), minn);
}
printf("Number of doors = %d\n",minn);
}
return 0;
}