hdojssworld VS DDD【概率dp求概率】

前端之家收集整理的这篇文章主要介绍了hdojssworld VS DDD【概率dp求概率】前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

ssworld VS DDD

Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1989Accepted Submission(s): 383


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


题意:两人转骰子前人小于后人点数前人hp-1否则后人hp-1;问前人胜概率。

#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
double dp[1500][1500];//开到20000超内存 
double p[6][2];
int main()
{
	int hs,hd,i,j;
	while(scanf("%d%d",&hd,&hs)!=EOF){
		memset(dp,sizeof(dp));
		for(i=0;i<2;++i){
			for(j=0;j<6;++j){
				scanf("%lf",&p[j][i]);
			}
		}
		double p1=0,p2=0;
		for(i=1;i<6;++i){
			for(j=0;j<i;++j){
				p1=p1+p[i][0]*p[j][1];
				p2=p2+p[j][0]*p[i][1];
			}
		}
		if(p1==0){printf("%.6lf\n",0);continue;}
		double pr=p1+p2;
		p1=p1/pr;
		p2=p2/pr;
		dp[0][0]=1;
		for(i=0;i<hd;++i){
			for(j=0;j<hs;++j){
				if(i)
					dp[i][j]+=dp[i-1][j]*p1;
				if(j)
					dp[i][j]+=dp[i][j-1]*p2;
			}
		}
		pr=0;
		for(i=0;i<hs;++i)pr+=dp[hd-1][i]*p1;
		printf("%.6lf\n",pr);
	}
	return 0;
}
原文链接:https://www.f2er.com/javaschema/284511.html

猜你在找的设计模式相关文章