ZOJ-3720 Magnet Darts 计算几何,概率

  题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3720

  题意:在一个矩形区域投掷飞镖,每个整点有磁性,每个点的磁性是一样的,因此飞镖只会落在整点上,投到每个点的得分是:Ax+By。矩形区域里面有个多边形,如果飞镖投在多边形里面则得分,求最终的得分期望。

  对于每个点,以它为中心的边长为1的正方形范围内,它都可以把飞镖吸引过来,则最后飞镖能得分的面积就是多边形内以及多边形上所有整点的正方形的面积并,然后期望公式E(X)=p*xi。。

  1 //STATUS:C++_AC_900MS_188KB

  2 #include <functional>

  3 #include <algorithm>

  4 #include <iostream>

  5 //#include <ext/rope>

  6 #include <fstream>

  7 #include <sstream>

  8 #include <iomanip>

  9 #include <numeric>

 10 #include <cstring>

 11 #include <cassert>

 12 #include <cstdio>

 13 #include <string>

 14 #include <vector>

 15 #include <bitset>

 16 #include <queue>

 17 #include <stack>

 18 #include <cmath>

 19 #include <ctime>

 20 #include <list>

 21 #include <set>

 22 #include <map>

 23 using namespace std;

 24 //using namespace __gnu_cxx;

 25 //define

 26 #define pii pair<int,int>

 27 #define mem(a,b) memset(a,b,sizeof(a))

 28 #define lson l,mid,rt<<1

 29 #define rson mid+1,r,rt<<1|1

 30 #define PI acos(-1.0)

 31 //typedef

 32 //typedef __int64 LL;

 33 //typedef unsigned __int64 ULL;

 34 //const

 35 const int N=35;

 36 const int INF=0x3f3f3f3f;

 37 const int MOD=100000,STA=8000010;

 38 //const LL LNF=1LL<<60;

 39 const double EPS=1e-8;

 40 const double OO=1e15;

 41 const int dx[4]={-1,0,1,0};

 42 const int dy[4]={0,1,0,-1};

 43 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

 44 //Daily Use ...

 45 inline int sign(double x){return (x>EPS)-(x<-EPS);}

 46 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

 47 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

 48 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}

 49 template<class T> inline T Min(T a,T b){return a<b?a:b;}

 50 template<class T> inline T Max(T a,T b){return a>b?a:b;}

 51 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

 52 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

 53 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

 54 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

 55 //End

 56 

 57 struct Node{

 58     double x,y;

 59 }nod[N];

 60 

 61 struct DNode{

 62     double x,y;

 63 }ju[2];

 64 

 65 double A,B;

 66 int n;

 67 

 68 int chaji(Node &a,Node &b){

 69     return a.x*b.y-b.x*a.y;

 70 }

 71 

 72 int ponls(Node &a,Node &b,Node &p)

 73 {

 74     if( (p.x==a.x && p.y==a.y) || (p.x==b.x && p.y==b.y) )return 2;

 75     Node r1,r2;

 76     r1.x=a.x-b.x,r1.y=a.y-b.y;

 77     r2.x=p.x-b.x,r2.y=p.y-b.y;

 78     if(!chaji(r1,r2) && p.x>=min(a.x,b.x) && p.x<=max(a.x,b.x)

 79         && p.y>=min(a.y,b.y) && p.y<=max(a.y,b.y))

 80         return 1;

 81     return 0;

 82 }

 83 

 84 int quick(Node &l1,Node &l2,Node &r1,Node &r2)

 85 {

 86 

 87     if(min(l1.x,l2.x)>max(r1.x,r2.x)

 88         || min(l1.y,l2.y)>max(r1.y,r2.y)

 89         || max(l1.x,l2.x)<min(r1.x,r2.x)

 90         || max(l1.y,l2.y)<min(r1.y,r2.y))

 91         return 0;

 92     return 1;

 93 }

 94 

 95 int las(Node &l1,Node &l2,Node &r1,Node &r2)

 96 {

 97     Node a,b,c;

 98     a.x=l1.x-r1.x;

 99     a.y=l1.y-r1.y;

100     b.x=r2.x-r1.x;

101     b.y=r2.y-r1.y;

102     c.x=l2.x-r1.x;

103     c.y=l2.y-r1.y;

104     if( ((a.x*b.y)-(b.x*a.y))*((c.x*b.y)-(b.x*c.y))<0)return 1;

105     else return 0;

106 }

107 

108 int pinply(int num_node,Node nod[],Node &p)

109 {

110     int i,j,cou=0;

111     Node ray;

112     ray.x=-1,ray.y=p.y;

113     for(i=0;i<num_node;i++){

114         j=(i+1)%num_node;

115         if(ponls(nod[i],nod[j],p))return 0;

116         if(nod[i].y!=nod[j].y){

117             if(ponls(p,ray,nod[i]) && nod[i].y==max(nod[i].y,nod[j].y))

118                 cou++;

119             else if(ponls(p,ray,nod[j]) && nod[j].y==max(nod[i].y,nod[j].y))

120                 cou++;

121             else if(quick(nod[i],nod[j],p,ray) && las(nod[i],nod[j],p,ray)

122                 && las(p,ray,nod[i],nod[j]))

123                 cou++;

124         }

125     }

126     return cou&1;

127 }

128 

129 bool isonline(int n,Node nod[],Node &p)

130 {

131     int i,j;

132     for(i=0;i<n;i++){

133         if( (p.y-nod[i].y)*(nod[i+1].x-nod[i].x)==(nod[i+1].y-nod[i].y)*(p.x-nod[i].x)

134            && p.x>=Min(nod[i].x,nod[i+1].x) && p.x<=Max(nod[i].x,nod[i+1].x)

135            && p.y>=Min(nod[i].y,nod[i+1].y) && p.y<=Max(nod[i].y,nod[i+1].y) )return true;

136     }

137     if( (p.y-nod[n].y)*(nod[0].x-nod[n].x)==(nod[0].y-nod[n].y)*(p.x-nod[n].x)

138        && p.x>=Min(nod[n].x,nod[0].x) && p.x<=Max(nod[n].x,nod[0].x)

139        && p.y>=Min(nod[n].y,nod[0].y) && p.y<=Max(nod[n].y,nod[0].y) )return true;

140     return false;

141 }

142 

143 double gets(Node &t)

144 {

145     double w,h;

146     w=Min(t.x+0.5,ju[1].x)-Max(t.x-0.5,ju[0].x);

147     h=Min(t.y+0.5,ju[1].y)-Max(t.y-0.5,ju[0].y);

148 //    printf("  %lf %lf\n",w,h);

149     return h*w;

150 }

151 

152 int main()

153 {

154  //   freopen("in.txt","r",stdin);

155     int i,j;

156     double ans,S;

157     Node t;

158     int min_x,max_x,min_y,max_y;

159     while(~scanf("%lf%lf%lf%lf",&ju[0].x,&ju[0].y,&ju[1].x,&ju[1].y))

160     {

161         scanf("%d%lf%lf",&n,&A,&B);

162         min_x=510,max_x=0,min_y=510,max_y=0;

163         for(i=0;i<n;i++){

164             scanf("%lf%lf",&nod[i].x,&nod[i].y);

165             min_x=Min(min_x,(int)nod[i].x);

166             max_x=Max(max_x,(int)nod[i].x);

167             min_y=Min(min_y,(int)nod[i].y);

168             max_y=Max(max_y,(int)nod[i].y);

169 

170         }

171         S=(ju[1].x-ju[0].x)*(ju[1].y-ju[0].y);

172 

173         ans=0;

174         for(i=min_x;i<=max_x;i++){

175             for(j=min_y;j<=max_y;j++){

176                 t.x=i,t.y=j;

177                 if(pinply(n,nod,t) || isonline(n-1,nod,t)){

178                     ans+=(A*i+B*j)*gets(t);

179                 }

180             }

181         }

182 

183         printf("%.3lf\n",ans/S);

184     }

185     return 0;

186 }

 

你可能感兴趣的:(net)