码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3076 ssworld VS DDD(概率dp)

时间:2014-10-13 23:44:37      阅读:404      评论:0      收藏:0      [点我收藏+]

标签:dp   概率dp   hdu   

题目链接: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

题意:

有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)

标签:dp   概率dp   hdu   

原文地址:http://blog.csdn.net/u012860063/article/details/40050165

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!