CodeForces - 540D (probability dp)

Title Source: the Click
given a certain number of rock, paper, scissors, respectively, the number of r, s, p, stone kill scissors, scissors to kill cloth that kill stone.
Q. The last camp probability of victory is.
dp [i] [j] [ k]: i represents the probability that there is a rock of k j scissor cloth.
Transfer equation can be directly identify probabilities, since there are a plurality of states can be transferred to dp [i] [j] [ k], it is an addition, see the specific code.

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<cstdlib>
#include<istream>
#include<vector>
#include<stack>
#include<map>
#include<algorithm>
#include<queue>
#define MAX_len 50100*4
using namespace std;
typedef long long ll;
double dp[102][102][102];// i个r j个s k个p
int main()
{
    memset(dp,0,sizeof(dp));
    int r,s,p;
    scanf("%d %d %d",&r,&s,&p);
    int i,j,k;
    dp[r][s][p]=1.0;
    for(i=r;i>=1;i--)//注意循环里不要到0,到0的话就已经到出现结果了。
    {
        for(j=s;j>=1;j--)
        {
            for(k=p;k>=1;k--)
            {
                int sum=i*j+i*k+j*k;
                if(sum==0)
                    break;
                if(i&&k)
                dp[i-1][j][k]+=dp[i][j][k]*(double(i*k*1.0)/double(sum*1.0));
                if(i&&j)
                dp[i][j-1][k]+=dp[i][j][k]*(double(i*j*1.0)/double(sum*1.0));
                if(j&&k)
                dp[i][j][k-1]+=dp[i][j][k]*(double(k*j*1.0)/double(sum*1.0));
            }
        }
    }
    double ans1=0.0,ans2=0.0,ans3=0.0;
    for(i=1;i<=100;i++)
    {
        for(j=0;j<=100;j++)
        {
            ans1+=dp[i][j][0];
            ans2+=dp[0][i][j];
            ans3+=dp[j][0][i];
        }
    }
    printf("%.9lf %.9lf %.9lf",ans1,ans2,ans3);
    return 0;
}

Published 72 original articles · won praise 19 · views 7506

Guess you like

Origin blog.csdn.net/weixin_43958964/article/details/104262873