无聊翻题目的时候发现这题完全照抄 UVA11188 奇怪的歌剧院 A Strange Opera House,样例都没改,感兴趣的可以去看看。
用展开法做反射:
一条在多边形内按镜面反射传播的声线,等价于把声源点依次关于反射到的边做对称,得到一个虚拟声源 ,然后声线在平面里就是从 出发的直线。
所以问题变成:从声源(图中黑点) 出发,允许最多 次反射,问边界上哪些点能被某条声线“第一次/下一次”打到(打到该点观众就能听到),并求这些点在墙上的总长度。
考虑: BFS /队列枚举反射层,每个状态表示一束声线:
-
虚拟声源 。
-
这束声线必须先穿过某条边 上的一个窗口段 (用边参数 表示)。
-
已用反射次数 。
从该状态出发,所有从 射出、且会穿过窗口段的射线,在多边形里继续走,都会下一次打到某条边上的一段区间;把这些区间加入 ans,并把每个命中的区间作为下一层反射的窗口,构造新虚拟声源 (关于该命中边对称)继续扩展,直到 。
如何从一个状态求下一次命中哪些边的哪些段?
对固定点 ,射线最先命中哪条边只会在射线方向扫过某个顶点时发生变化,所以可以按角度做分段:
-
根状态(无窗口)用所有顶点相对的极角排序,得到若干角区间。
-
有窗口时,用窗口两端点的极角作为起止,再把落在这个角扇形里的顶点极角加入分割。
-
对每个角区间 ,取区间内部两条方向(略偏离端点)各发一条射线,算到边界的下一次交点,若两次命中的是同一条边,就得到该边上的一个参数区间,加入即可。
最后对每条边把收集到的参数区间合并,乘边长求和即可。
参考代码
注意代码和 UVA11188 基本相同,由于输出要求微调,存在无用变量 tc。
#include<bits/stdc++.h>
using namespace std;
using ld=long double;
static const ld PI=acosl(-1.0L);
static const ld TP=2*PI;
struct P{
ld x,y;
};
static inline P operator+(P a,P b){
return {a.x+b.x,a.y+b.y};
}
static inline P operator-(P a,P b){
return {a.x-b.x,a.y-b.y};
}
static inline P operator*(P a,ld k){
return {a.x*k,a.y*k};
}
static inline ld dot(P a,P b){
return a.x*b.x+a.y*b.y;
}
static inline ld cr(P a,P b){
return a.x*b.y-a.y*b.x;
}
static inline ld n2(P a){
return dot(a,a);
}
static inline ld dist(P a,P b){
ld dx=a.x-b.x,dy=a.y-b.y;
return sqrtl(dx*dx+dy*dy);
}
static inline ld nrm(ld a){
a=fmodl(a,TP);
if(a<0) a+=TP;
return a;
}
static inline P rf(P p,P a,P b){
P v=b-a;
ld t=dot(p-a,v)/n2(v);
P h=a+v*t;
return h*2-p;
}
static inline bool rseg(P q,P d,P a,P b,ld &t,ld &u){
P r=d,s=b-a,w=a-q;
ld den=cr(r,s);
if(fabsl(den)<1e-18L) return false;
t=cr(w,s)/den;
u=cr(w,r)/den;
if(t<-1e-12L) return false;
if(u<-1e-12L||u>1+1e-12L) return false;
return true;
}
struct Hit{
int e;
ld u;
};
struct Solver{
int n,k;
vector<P> p;
bool hit(P q,ld ang,bool he,int ei,ld lo,ld hi,Hit &h){
P d={cosl(ang),sinl(ang)};
ld t0=0;
if(he){
P a=p[ei],b=p[(ei+1)%n];
ld te,ue;
if(!rseg(q,d,a,b,te,ue)) return false;
if(ue<lo-1e-10L||ue>hi+1e-10L) return false;
t0=te;
}
ld bt=1e100L;
int be=-1;
ld bu=0;
for(int i=0;i<n;i++){
P a=p[i],b=p[(i+1)%n];
ld t,u;
if(!rseg(q,d,a,b,t,u)) continue;
if(t<=t0+1e-10L) continue;
if(t<bt){
bt=t;
be=i;
bu=u;
}
}
if(be<0) return false;
h.e=be;
h.u=min<ld>(1,max<ld>(0,bu));
return true;
}
static inline void uq(vector<ld> &a){
sort(a.begin(),a.end());
vector<ld> b;
b.reserve(a.size());
for(ld x:a) if(b.empty()||fabsl(x-b.back())>1e-12L) b.push_back(x);
a.swap(b);
}
void add(P q,bool he,int ei,ld lo,ld hi,ld L,ld R,int dep,vector<vector<pair<ld,ld>>> &tmp){
if(R-L<1e-14L||dep>25) return;
ld eps=min<ld>(1e-10L,(R-L)/10);
if(eps*2>=R-L) eps=(R-L)/3;
Hit h1,h2;
bool ok1=hit(q,L+eps,he,ei,lo,hi,h1);
bool ok2=hit(q,R-eps,he,ei,lo,hi,h2);
if(!ok1&&!ok2) return;
if(ok1&&ok2&&h1.e==h2.e){
ld l=min(h1.u,h2.u),r=max(h1.u,h2.u);
if(r-l>1e-12L) tmp[h1.e].push_back({max<ld>(0,l),min<ld>(1,r)});
}
else{
ld M=(L+R)/2;
add(q,he,ei,lo,hi,L,M,dep+1,tmp);
add(q,he,ei,lo,hi,M,R,dep+1,tmp);
}
}
vector<vector<pair<ld,ld>>> sweep(P q,bool he,int ei,ld lo,ld hi){
vector<vector<pair<ld,ld>>> tmp(n);
if(!he){
vector<ld> a;
a.reserve(n);
for(int i=0;i<n;i++) a.push_back(nrm(atan2l(p[i].y-q.y,p[i].x-q.x)));
uq(a);
a.push_back(a[0]+TP);
for(size_t i=0;i+1<a.size();i++) add(q,false,0,0,0,a[i],a[i+1],0,tmp);
}
else{
P a=p[ei],b=p[(ei+1)%n];
P w1=a+(b-a)*lo,w2=a+(b-a)*hi;
ld A1=nrm(atan2l(w1.y-q.y,w1.x-q.x));
ld A2=nrm(atan2l(w2.y-q.y,w2.x-q.x));
ld d=A2-A1;
while(d<0) d+=TP;
while(d>=TP) d-=TP;
if(d>PI){
swap(A1,A2);
d=TP-d;
}
if(d<1e-14L) return tmp;
ld st=A1,en=A1+d;
vector<ld> ang;
ang.reserve(n+2);
ang.push_back(st);
ang.push_back(en);
for(int i=0;i<n;i++){
ld v=nrm(atan2l(p[i].y-q.y,p[i].x-q.x));
if(v<st-1e-12L) v+=TP;
if(v>st+1e-12L&&v<en-1e-12L) ang.push_back(v);
}
uq(ang);
for(size_t i=0;i+1<ang.size();i++) add(q,true,ei,lo,hi,ang[i],ang[i+1],0,tmp);
}
for(int i=0;i<n;i++){
auto &v=tmp[i];
if(v.empty()) continue;
sort(v.begin(),v.end());
vector<pair<ld,ld>> m;
m.reserve(v.size());
for(auto [l,r]:v){
if(m.empty()||l>m.back().second+1e-12L) m.push_back({l,r});
else m.back().second=max(m.back().second,r);
}
v.swap(m);
}
return tmp;
}
};
struct St{
int d,e;
ld l,r;
P q;
};
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n,tc=1;
while(cin>>n){
if(n==0) break;
Solver sv;
sv.n=n;
int k;
ld x,y;
cin>>k>>x>>y;
sv.k=k;
sv.p.resize(n);
for(int i=0;i<n;i++){
ld xi,yi;
cin>>xi>>yi;
sv.p[i]={xi,yi};
}
P S={x,y};
vector<vector<pair<ld,ld>>> ans(n);
deque<St> q;
auto root=sv.sweep(S,false,0,0,0);
for(int e=0;e<n;e++) for(auto [l,r]:root[e]){
ans[e].push_back({l,r});
if(k>=1&&r-l>1e-12L) q.push_back({1,e,l,r,rf(S,sv.p[e],sv.p[(e+1)%n])});
}
while(!q.empty()){
auto cur=q.front();
q.pop_front();
auto nxt=sv.sweep(cur.q,true,cur.e,cur.l,cur.r);
for(int e=0;e<n;e++) for(auto [l,r]:nxt[e]){
ans[e].push_back({l,r});
if(cur.d<k&&r-l>1e-12L) q.push_back({cur.d+1,e,l,r,rf(cur.q,sv.p[e],sv.p[(e+1)%n])});
}
}
ld tot=0;
for(int i=0;i<n;i++){
auto &v=ans[i];
if(v.empty()) continue;
sort(v.begin(),v.end());
vector<pair<ld,ld>> m;
m.reserve(v.size());
for(auto [l,r]:v){
l=max<ld>(0,l);
r=min<ld>(1,r);
if(r-l<1e-12L) continue;
if(m.empty()||l>m.back().second+1e-12L) m.push_back({l,r});
else m.back().second=max(m.back().second,r);
}
ld L=dist(sv.p[i],sv.p[(i+1)%n]);
for(auto [l,r]:m) tot+=(r-l)*L;
}
cout.setf(std::ios::fixed);
cout<<setprecision(2)<<(double)tot+1e-8<<"\n";
}
return 0;
}