有一个100*100的密室,密室中贯穿了n个墙,再加上外围的四个墙,这些墙不会有三个及以上的墙交于一点
给出密室中宝藏的坐标,要从密室外爆破通过墙壁到达宝藏位置,爆破位置必须是进入房间墙壁的中心点
问最少需要爆破墙壁数
因为必须是墙壁中心点,我们可以从外围寻找两两点构成墙壁的中心点,从这些位置开始爆破
如果从这些点和宝藏连一条线,可以发现,与这条线相交的墙壁都是必须要爆破的,而且只需要一次
不相交的就不需要爆破,所以答案就是这些线与墙壁相交最少的个数+1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long ll;
const double eps=1e-8;
const double PI=acos(-1.0);
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 _x,double _y)
{
x=_x;y=_y;
}
Point operator - (const Point &b) const
{
return Point(x-b.x,y-b.y);
}
//叉积
double operator ^ (const Point &b) const
{
return x*b.y-y*b.x;
}
//点积
double operator * (const Point &b) const
{
return x*b.x+y*b.y;
}
///绕原点旋转角度B(弧度值),后x,y的变化
void transXY(double B)
{
double tx=x,ty=y;
x=tx*cos(B)-ty*sin(B);
y=tx*sin(B)+ty*cos(B);
}
};
struct Line
{
Point s,e;
Line(){}
Line(Point _s,Point _e) { s = _s;e = _e; }
//两直线相交求交点
//第一个值为0表示直线重合,为1表示平行,为0表示相交,为2是相交
//只有第一个值为2时,交点才有意义
pair<int,Point> operator &(const Line &b)const
{
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == 0)
{
if(sgn((s-b.e)^(b.s-b.e)) == 0)
return make_pair(0,res);//重合
else return make_pair(1,res);//平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x-s.x)*t;
res.y += (e.y-s.y)*t;
return make_pair(2,res);
}
}l[50];
//*判断线段相交
bool inter(Line l1,Line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.e)^(l1.s-l1.e))*sgn((l2.e-l1.e)^(l1.s-l1.e)) <= 0 &&
sgn((l1.s-l2.e)^(l2.s-l2.e))*sgn((l1.e-l2.e)^(l2.s-l2.e)) <= 0;
}
int n,topt,dowt,lefct,rigt;
double top[50],dow[50],lef[50],rig[50];
int main()
{
while(~scanf("%d",&n))
{
topt=dowt=lefct=rigt=0;
for(int i=0;i<n;i++)
{
scanf("%lf%lf%lf%lf",&l[i].s.x,&l[i].s.y,&l[i].e.x,&l[i].e.y);
if(l[i].s.x==0.0) lef[lefct++]=l[i].s.y;
if(l[i].s.x==100.0) rig[rigt++]=l[i].s.y;
if(l[i].s.y==0.0) dow[dowt++]=l[i].s.x;
if(l[i].s.y==100.0) top[topt++]=l[i].s.x;
if(l[i].e.x==0.0) lef[lefct++]=l[i].e.y;
if(l[i].e.x==100.0) rig[rigt++]=l[i].e.y;
if(l[i].e.y==0.0) dow[dowt++]=l[i].e.x;
if(l[i].e.y==100.0) top[topt++]=l[i].e.x;
}
top[topt++]=dow[dowt++]=lef[lefct++]=rig[rigt++]=0.0;
top[topt++]=dow[dowt++]=lef[lefct++]=rig[rigt++]=100.0;
sort(top,top+topt);
sort(dow,dow+dowt);
sort(lef,lef+lefct);
sort(rig,rig+rigt);
int ans=n;
Point t;
scanf("%lf%lf",&t.x,&t.y);
for(int i=1;i<topt;i++)
{
Line ll=Line(Point((top[i]+top[i-1])/2.0,100.0),t);
int cnt=0;
for(int j=0;j<n;j++)
if(inter(ll,l[j])) cnt++;
ans=min(ans,cnt);
}
for(int i=1;i<dowt;i++)
{
Line ll=Line(Point((dow[i]+dow[i-1])/2.0,0.0),t);
int cnt=0;
for(int j=0;j<n;j++)
if(inter(ll,l[j])) cnt++;
ans=min(ans,cnt);
}
for(int i=1;i<lefct;i++)
{
Line ll=Line(Point(0.0,(lef[i]+lef[i-1])/2.0),t);
int cnt=0;
for(int j=0;j<n;j++)
if(inter(ll,l[j])) cnt++;
ans=min(ans,cnt);
}
for(int i=1;i<rigt;i++)
{
Line ll=Line(Point(100.0,(rig[i]+rig[i-1])/2.0),t);
int cnt=0;
for(int j=0;j<n;j++)
if(inter(ll,l[j])) cnt++;
ans=min(ans,cnt);
}
printf("Number of doors = %d\n",ans+1);
}
return 0;
}
1095




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



