Random Point in Triangle【隨機數解決期望值問題】

Random Point in Triangle

題目鏈接(點擊)

題目描述

Bobo has a triangle ABC with A(x1,y1),B(x2,y2)A(x1,y1),B(x2,y2) and C(x3,y3)C(x3,y3). Picking a point P uniformly in triangle ABC, he wants to know the expectation value E=max{SPAB,SPBC,SPCA}E=max{SPAB,SPBC,SPCA} where SXYZSXYZ denotes the area of triangle XYZ.

Print the value of 36×E36×E. It can be proved that it is always an integer.

輸入描述:

The input consists of several test cases and is terminated by end-of-file.

Each test case contains six integers x1,y1,x2,y2,x3,y3x1,y1,x2,y2,x3,y3.

* |x1|,|y1|,|x2|,|y2|,|x3|,|y3|≤108|x1|,|y1|,|x2|,|y2|,|x3|,|y3|≤108
* There are at most 105105 test cases.

輸出描述:

For each test case, print an integer which denotes the result.

輸入

0 0 1 1 2 2
0 0 0 0 1 1
0 0 0 0 0 0

輸出

0
0
0

題目描述:

      多組輸入三角形各個頂點座標p1,p2,p3,在三角形中任取一點p,計算 期望E=max(S(p,p1,p2),max(S(p,p1,p3),S(p,p2,p3)));

思路:

   1、產生隨機點,選出在三角形內部的點,對每個符合條件的點分別求出最大面積並求和,最後取面積和的平均值即爲期望E。     (當隨機數足夠多時,所有最大面積和的平均值即爲期望)

   2、根據下面找規律代碼可以求出:(由於是跑的隨機數,結果定不完全等於22)

   3、求出E後可以看出規律,即 36*E=22*S(p1,p2,p3)

補充:

    1、根據三角形座標求三角形面積:向量差乘求三角形面積

    2、座標點結構體:

struct poLL{
    LL x;
    LL y;
};
struct poLL p1,p2,p3,p;
p1.x=x1,p1.y=y1;
p2.x=x2,p2.y=y2;
p3.x=x3,p3.y=y3;

   一看就能看懂,挺好用的,在傳點座標時不用分別傳x、y了 

找規律代碼:

#include<bits/stdc++.h>
using namespace std;
struct point{
    double x;
    double y;
};
double S(point p1,point p2,point p3) ///向量叉乘求三角形面積可以看上面鏈接
{
    return fabs((p1.x-p3.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p1.y-p3.y));
}

bool judge(point p,point p1,point p2,point p3) ///判斷生成的隨機點是不是在三角中 原理是:三個 
                                         ///小三角形面積之和是不是等於原來大的三角形面積
{
    if(S(p1,p2,p3)==(S(p1,p2,p)+S(p,p1,p3)+S(p2,p3,p))){
        //printf("*%lf %lf %lf %lf\n",S(p1,p2,p3),S(p1,p2,p),S(p,p1,p3),S(p2,p3,p));
        return true;
    }
    return false;
}
int main()
{
    double x1,x2,x3,y1,y2,y3;
    while(scanf("%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF){
        double cnt=0;
        struct point p1,p2,p3,p;
        p1.x=x1,p1.y=y1;
        p2.x=x2,p2.y=y2;
        p3.x=x3,p3.y=y3;
        srand(time(0));
        double sum=0;
        for(int i=0;i<100000000;i++){
            int x=rand();
            int y=rand();
            p.x=x*1.0,p.y=y*1.0;
            if(judge(p,p1,p2,p3)){
                cnt++;
                sum+=(max(S(p1,p2,p),max(S(p,p1,p3),S(p2,p3,p))));
            }
            //printf("%.3lf\n",sum);
        }
        double S1=S(p1,p2,p3);
        double q=((sum/cnt)*36.0)/S1;
        printf("%.3lf\n",q);
    }
    return 0;
}

AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
struct poLL{
    LL x;
    LL y;
};
LL S(poLL p1,poLL p2,poLL p3)
{
    return abs((p1.x-p3.x)*(p1.y-p2.y)-(p1.x-p2.x)*(p1.y-p3.y));
}
int main()
{
    LL x1,x2,x3,y1,y2,y3;
    while(scanf("%lld%lld%lld%lld%lld%lld",&x1,&y1,&x2,&y2,&x3,&y3)!=EOF){
        struct poLL p1,p2,p3,p;
        p1.x=x1,p1.y=y1;
        p2.x=x2,p2.y=y2;
        p3.x=x3,p3.y=y3;
        printf("%lld\n",11*S(p1,p2,p3));
    }
    return 0;
}

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章