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

ZOJ 3329 One Person Game

时间:2015-08-27 00:36:01      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

题意:有三个骰子,分别有k1,k2,k3个面。
每次连续掷三个骰子,每个骰子的每个面出现的概率相等,如果三个面分别为a,b,c则分数置0,否则加上三个骰子的分数之和。

当分数大于n时结束。求游戏的期望步数。初始分数为0。


做法:

设dp[i]表示达到i分时到达目标状态的期望,pk为投掷k分的概率,p0为回到0的概率
则dp[i]=∑(pk*dp[i+k])+dp[0]*p0+1;
都和dp[0]有关系,而且dp[0]就是我们所求,为常数
设dp[i]=A[i]*dp[0]+B[i];
代入上述方程右边得到:
dp[i]=∑(pk*A[i+k]*dp[0]+pk*B[i+k])+dp[0]*p0+1
     =(∑(pk*A[i+k])+p0)dp[0]+∑(pk*B[i+k])+1;
     明显A[i]=(∑(pk*A[i+k])+p0)
     B[i]=∑(pk*B[i+k])+1
     先递推求得A[0]和B[0].
     那么  dp[0]=B[0]/(1-A[0]);

另外,关于边界问题,若i>n,因为dp[i]=A[i]*dp[0]+B[i]=0,A[i]>=0,B[i]>=0,dp[0]>0,所以A[i]=0,B[i]=0。

于是就可以递推了。

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
double x[510],y[510];
int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int n,k1,k2,k3,a,b,c;
		cin>>n>>k1>>k2>>k3>>a>>b>>c;
		double p=1.0/(k1*k2*k3);
		for(int i=n;i>-1;i--)
		{
			x[i]=p;
			y[i]=1;
			for(int j=1;j<=k1;j++)
				for(int k=1;k<=k2;k++)
					for(int ii=1;ii<=k3;ii++)
						if((j!=a||k!=b||ii!=c)&&i+j+k+ii<=n)
						{
							x[i]+=x[i+j+k+ii]*p;
							y[i]+=y[i+j+k+ii]*p;
						}
		}		
		printf("%.9f\n",y[0]/(1-x[0]));
	}
}

Time Limit: 1 Second      Memory Limit: 32768 KB      Special Judge

There is a very simple and interesting one-person game. You have 3 dice, namely Die1Die2 and Die3Die1 has K1 faces. Die2 has K2 faces. Die3 has K3 faces. All the dice are fair dice, so the probability of rolling each value, 1 to K1K2K3 is exactly 1 / K1, 1 / K2 and 1 / K3. You have a counter, and the game is played as follow:

  1. Set the counter to 0 at first.
  2. Roll the 3 dice simultaneously. If the up-facing number of Die1 is a, the up-facing number of Die2 is b and the up-facing number of Die3 is c, set the counter to 0. Otherwise, add the counter by the total value of the 3 up-facing numbers.
  3. If the counter‘s number is still not greater than n, go to step 2. Otherwise the game is ended.

Calculate the expectation of the number of times that you cast dice before the end of the game.

Input

There are multiple test cases. The first line of input is an integer T (0 < T <= 300) indicating the number of test cases. Then T test cases follow. Each test case is a line contains 7 non-negative integers nK1K2K3abc (0 <= n <= 500, 1 < K1K2K3 <= 6, 1 <= a <= K1, 1 <= b <= K2, 1 <= c <= K3).

Output

For each test case, output the answer in a single line. A relative error of 1e-8 will be accepted.

Sample Input

2
0 2 2 2 1 1 1
0 6 6 6 1 1 1

Sample Output

1.142857142857143
1.004651162790698

Author: CAO, Peng
Source: The 7th Zhejiang Provincial Collegiate Programming Contest

版权声明:本文为博主原创文章,未经博主允许不得转载。

ZOJ 3329 One Person Game

标签:

原文地址:http://blog.csdn.net/stl112514/article/details/48012783

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