csu1377Putter && HOJ12816

时间:2021-07-23 01:33:03

链接:(csu)http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1377

   (HOJ)http://49.123.82.55/online/?action=problem&type=list&courseid=0&querytext=&pageno=57

题意:

  给定凸多边形的点(按顺时针),多边形内一点沿某个方向运动,碰到边进行镜面反射,问在一条边不碰超过两次的情况下,有多少种不同的碰撞方法。多边形最多8条边。

思路:

  枚举每一种情况,合理就对结果+1,枚举用dfs轻松加愉快。本题关键在于求出镜像点,然后以镜像点出发,对边进行考察,若能直线到达,则该点能被反射到,否则不能。

Code:

 #include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define op operator
#define cp const P&
#define cn const
#define db double
#define rt return
using namespace std;
cn db eps = 1e-;
cn db pi = acos(-1.0);
inline int sig(db x) {return (x>eps) - (x<-eps);} struct P{
db x, y;
P(db a = 0.0, db b = 0.0) : x(a), y(b) {}
void in() { scanf("%lf %lf", &x, &y);}
void out(){ printf("%lf %lf\n", x, y);}
bool op<(cp a)cn {return sig(x-a.x) ? sig(x-a.x) : sig(y-a.y);}
P op-(cp a)cn {return P(x-a.x, y-a.y);}
P op+(cp a)cn {return P(x+a.x, y+a.y);}
db op^(cp a)cn {return x*a.y - y*a.x;}
db cross(P a, P b) {return (a-*this) ^ (b-*this);}
db op*(cp a)cn {return x*a.x + y*a.y;} //点积
db dot(P a, P b) {return (a-*this) * (b-*this);}
P op*(cn db &a)cn {return P(a*x, a*y);}
bool op/(cp a)cn {return sig(x*a.y - y*a.x) == ;}
db dis() {return sqrt(x*x + y*y);}
db dis(P a) {return (a-*this).dis();}
P T() {return P(-y, x);}
P roate(db d) {
return P(x*cos(d) - y*sin(d), y*cos(d) + x*sin(d));
}
bool on(P a, P b) {return !sig(cross(a, b)) && sig(dot(a, b)) <= ;} //点在线段上
P Mirror_P(P a, P b) {
P v1 = *this - a, v2 = b - a;
db w = acos(v1*v2 / v1.dis() / v2.dis());
int f = sig(v1^v2);
w = *w*f;
rt a + v1.roate(w);
}
}p[], q;
inline P inset(P a1, P b1, P a2, P b2) {
db u = (b1^a1), z = b2^a2, w = (b1-a1) ^ (b2-a2);
P v;
v.x = ((b2.x-a2.x)*u - (b1.x-a1.x)*z) / w;
v.y = ((b2.y-a2.y)*u - (b1.y-a1.y)*z) / w;
rt v;
}
bool vis[];
int ans, n;
bool if_insight(P q, int i, P a, P b) {
if(sig(q.cross(b, p[i])) <= && sig(q.cross(b, p[i+])) <= ) rt false;
if(sig(q.cross(a, p[i])) >= && sig(q.cross(a, p[i+])) >= ) rt false;
rt true;
}
void dfs(P q, int k, int m, P a, P b) {
if(vis[k]) rt ;
if(m == n) { ++ans; rt ; }
vis[k] = ;
q = q.Mirror_P(p[k], p[k+]);
for(int i = ; i < n; ++i) {
if(i != k && if_insight(q, i, a, b)) {
P c , d;
if(sig(q.cross(b, p[i])) < ) c = inset(b, q, p[i], p[i+]);
else c = p[i];
if(sig(q.cross(a, p[i+])) > ) d = inset(a, q, p[i], p[i+]);
else d = p[i+]; dfs(q, i, m+, c, d);
}
}
vis[k] = ;
}
void solve() {
p[n] = p[];
ans = ;
for(int i = ; i < n; ++i)
dfs(q, i, , p[i], p[i+]);
printf("%d\n", ans);
rt ;
}
int main()
{
while(scanf("%d", &n), n) {
q.in();
for(int i = ; i < n; ++i) p[i].in();
memset(vis, , sizeof vis);
solve();
}
return ;
}