方法:计算几何
思路:将墙的端点与藏宝图所在顶点所构成的线段与所有的墙壁构成的线段进行判断,
看是否相交,找出交点个数最小的就是最少需要开门的个数。本题在nyist上不能直接
使用如下代码,会超时:
for(i=0;i<n;i++)
{
node1=0;node2=0;
for(j=0;j<n;j++)
{
if(is_cross(ss[i].s,p,ss[j].s,ss[j].e))node1++;
if(is_cross(ss[i].e,p,ss[j].s,ss[j].e))node2++;
}
if(node1<min)min=node1;
if(node2<min)min=node2;
}而要先用一个结构体数组将其先保存起来,再进行比较,详见代码一。
至于按不按极角排序,其实影响不大。
代码一:线段相交+枚举
#include <iostream>
#include <algorithm>
using namespace std;
const int N=105;
struct point { double x,y; };
struct line { point s,e; };
point p[1]={0,0};
double multi(point p0,point p1,point p2)
{ return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);}
bool segment_cross(point s1,point e1,point s2,point e2)
{
return max(s1.x,e1.x)>=min(s2.x,e2.x)&&
max(s2.x,e2.x)>=min(s1.x,e1.x)&&
max(s1.y,e1.y)>=min(s2.y,e2.y)&&
max(s2.y,e2.y)>=min(s1.y,e1.y)&&
multi(s2,e1,s1)*multi(e1,e2,s1)>=0&&
multi(s1,e2,s2)*multi(e2,e1,s2)>=0;
}
bool cmp(const point &b,const point &c)// 注意p[0]是最左下的点
{
if((b.x-p[0].x) * (c.y-p[0].y) - (b.y-p[0].y)*(c.x-p[0].x)==0)
{
if(b.x == c.x) return b.y < c.y;
return b.x < c.x;
}
return (b.x-p[0].x) * (c.y-p[0].y) - (b.y-p[0].y)*(c.x-p[0].x) > 0;
}
int main()
{
int n,i,j=0;
line L[N];
point mid,pnt[2*N];
while(cin>>n)
{
for(i=0;i<n;i++)
{
cin>>L[i].s.x>>L[i].s.y>>L[i].e.x>>L[i].e.y;
pnt[j++]=L[i].s;pnt[j++]=L[i].e;
}
cin>>mid.x>>mid.y;
sort(pnt,pnt+2*n,cmp);
int min=10000;
for(i=0;i<2*n;i++)
{
int start=0;
for(j=0;j<n;j++)
if(segment_cross(pnt[i],mid,L[j].s,L[j].e))start++;
if(start<min)min=start;
}
if(n)cout<<"Number of doors = "<<min<<endl;
else cout<<"Number of doors = "<<1<<endl;
}
return 0;
}
代码二:http://www.cppblog.com/logics-space/archive/2009/07/24/91048.html
//
线段相交
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
double const EPS = 1e- 8;
const int INF = 1<< 30;
int dcmp( double x){ return x < -EPS ? - 1 : x > EPS;}
struct Point{
double x,y;
Point(){}
Point( double a, double b):x(a), y(b){}
bool operator<(Point a){ return atan2(y - 50, x - 50) < atan2(a.y - 50, a.x - 50); }
};
struct Line{Point a, b;};
Point P[ 128], s, t;
Line L[ 36];
int n, cnt, best;
double xmult(Point p1, Point p2 , Point p0)
{
return (p1.x - p0.x)*(p2.y - p0.y)-(p2.x - p0.x)*(p1.y - p0.y);
}
bool same_side(Point p1, Point p2, Line L)
{
return dcmp(xmult(L.b, p1, L.a) * xmult(L.b, p2, L.a)) >= 0;
}
int main()
{
int i, j, ans;
best = INF; cnt = 0;
P[cnt++] = Point( 0, 0);
P[cnt++] = Point( 100, 0);
P[cnt++] = Point( 0, 100);
P[cnt++] = Point( 100, 100);
cin >> n;
for(i = 0; i < n; i++)
{
cin>> L[i].a.x >> L[i].a.y >> L[i].b.x >> L[i].b.y;
P[cnt++] = L[i].a;
P[cnt++] = L[i].b;
}
cin>> s.x >> s.y;
sort(P, P+cnt);
for(i = 0; i < cnt; i++ )
{
ans = 0;
t = Point( (P[i].x + P[(i+ 1)%cnt].x)/ 2, (P[i].y + P[(i+ 1)%cnt].y)/ 2 );
for(j = 0; j < n; j++)
if(!same_side(s, t, L[j]))ans++;
if(ans < best) best = ans;
}
printf( " Number of doors = %d/n ", best+ 1);
}
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
double const EPS = 1e- 8;
const int INF = 1<< 30;
int dcmp( double x){ return x < -EPS ? - 1 : x > EPS;}
struct Point{
double x,y;
Point(){}
Point( double a, double b):x(a), y(b){}
bool operator<(Point a){ return atan2(y - 50, x - 50) < atan2(a.y - 50, a.x - 50); }
};
struct Line{Point a, b;};
Point P[ 128], s, t;
Line L[ 36];
int n, cnt, best;
double xmult(Point p1, Point p2 , Point p0)
{
return (p1.x - p0.x)*(p2.y - p0.y)-(p2.x - p0.x)*(p1.y - p0.y);
}
bool same_side(Point p1, Point p2, Line L)
{
return dcmp(xmult(L.b, p1, L.a) * xmult(L.b, p2, L.a)) >= 0;
}
int main()
{
int i, j, ans;
best = INF; cnt = 0;
P[cnt++] = Point( 0, 0);
P[cnt++] = Point( 100, 0);
P[cnt++] = Point( 0, 100);
P[cnt++] = Point( 100, 100);
cin >> n;
for(i = 0; i < n; i++)
{
cin>> L[i].a.x >> L[i].a.y >> L[i].b.x >> L[i].b.y;
P[cnt++] = L[i].a;
P[cnt++] = L[i].b;
}
cin>> s.x >> s.y;
sort(P, P+cnt);
for(i = 0; i < cnt; i++ )
{
ans = 0;
t = Point( (P[i].x + P[(i+ 1)%cnt].x)/ 2, (P[i].y + P[(i+ 1)%cnt].y)/ 2 );
for(j = 0; j < n; j++)
if(!same_side(s, t, L[j]))ans++;
if(ans < best) best = ans;
}
printf( " Number of doors = %d/n ", best+ 1);
}
本文介绍了一个计算几何问题的解决方法,通过线段相交判断来确定打开最少的门数,以便从房间的一侧到达另一侧。文章提供了两种不同的实现代码,并讨论了优化策略。
659

被折叠的 条评论
为什么被折叠?



