首页 > 代码库 > HDU 3076 ssworld VS DDD(概率dp)

HDU 3076 ssworld VS DDD(概率dp)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3076


Problem Description
One day, sssworld and DDD play games together, but there are some special rules in this games.
They both have their own HP. Each round they dice respectively and get the points P1 and P2 (1 <= P1, P2 <= 6). Small number who, whose HP to reduce 1, the same points will remain unchanged. If one of them becomes 0 HP, he loses. 
As a result of technical differences between the two, each person has different probability of throwing 1, 2, 3, 4, 5, 6. So we couldn’t predict who the final winner. 

 

Input
There are multiple test cases.
For each case, the first line are two integer HP1, HP2 (1 <= HP1, HP2 <= 2000), said the first player sssworld’s HP and the second player DDD’s HP. 
The next two lines each have six floating-point numbers per line. The jth number on the ith line means the the probability of the ith player gets point j. The input data ensures that the game always has an end. 
 

Output
One float with six digits after point, indicate the probability sssworld won the game.
 

Sample Input
5 5 1.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 1.000 5 5 0.000 0.000 0.000 0.000 0.000 1.000 1.000 0.000 0.000 0.000 0.000 0.000
 

Sample Output
0.000000 1.000000
 

Source
2009 Multi-University Training Contest 17 - Host by NUDT

题意:

有A、B个人分别有H1 H2的血量,他们轮流的扔骰子,仍的点数小的减一血,
若仍的点数一样则都不变,谁先到减0, 谁输,问A赢的概率。


代码如下:

#include <cstdio>
#include <cstring>
double dp[2017][2017];
//dp[i][j]表示A胜j次,B胜i次的概率。
int main()
{
    int n, m;
    double a[7], b[7];
    while(~scanf("%d%d",&m,&n))
    {
        memset(dp,0,sizeof(dp));
        double p1 = 0, p2 = 0, p = 0;
        //设p1,p2,p表示A赢,B赢,两者平局。
        for(int i = 1; i <= 6; i++)
        {
            scanf("%lf",&a[i]);
        }
        for(int i = 1; i <= 6; i++)
        {
            scanf("%lf",&b[i]);
        }
        for(int i = 2; i <= 6; i++)//A赢
        {
            for(int j = 1; j < i; j++)
            {
                p1+=a[i]*b[j];
            }
        }
        for(int j = 2; j <= 6; j++)//B赢
        {
            for(int i = 1; i < j; i++)
            {
                p2+=a[i]*b[j];
            }
        }
        double w1, w2;
        //w1,w2表示整个过程之中赢一局的概率
        p = 1-p1-p2;
        if(p == 1)
        {
            w1 = 0;
            w2 = 0;
        }
        else
        {
            w1 = p1/(1-p);
            w2 = p2/(1-p);
        }
        dp[0][0] = 1;
        for(int j = 0; j < m; j++)
        {
            for(int i = 0; i <= n; i++)
            {
                if(j > 0)
                    dp[i][j]+=dp[i][j-1]*w1;
                if(i > 0)
                    dp[i][j]+=dp[i-1][j]*w2;
            }
        }
        double maxx = 0;
        for(int i = 0; i < n; i++)
        {
            maxx+=dp[i][m-1]*w1;
        }
        printf("%.6lf\n",maxx);
    }
    return 0;
}



HDU 3076 ssworld VS DDD(概率dp)