Minimal Backgammon - POJ 3519 概率dp

来源:互联网 发布:cydia无法安装软件 编辑:程序博客网 时间:2024/04/29 19:00

Minimal Backgammon
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 1210 Accepted: 711 Special Judge

Description


Figure 2: An example game

Here is a very simple variation of the game backgammon, named “Minimal Backgammon”. The game is played by only one player, using only one of the dice and only one checker (the token used by the player).

The game board is a line of (N + 1) squares labeled as 0 (the start) to N (the goal). At the beginning, the checker is placed on the start (square 0). The aim of the game is to bring the checker to the goal (square N). The checker proceeds as many squares as the roll of the dice. The dice generates six integers from 1 to 6 with equal probability.

The checker should not go beyond the goal. If the roll of the dice would bring the checker beyond the goal, the checker retreats from the goal as many squares as the excess. For example, if the checker is placed at the square (N − 3), the roll “5” brings the checker to the square (N − 2), because the excess beyond the goal is 2. At the next turn, the checker proceeds toward the goal as usual.

Each square, except the start and the goal, may be given one of the following two special instructions.

  • Lose one turn (labeled “L” in Figure 2)
    If the checker stops here, you cannot move the checker in the next turn.

  • Go back to the start (labeled “B” in Figure 2)
    If the checker stops here, the checker is brought back to the start.

Given a game board configuration (the size N, and the placement of the special instructions), you are requested to compute the probability with which the game succeeds within a given number of turns.

Input

The input consists of multiple datasets, each containing integers in the following format.

N T L B
Lose
1

LoseL
Back
1

BackB

N is the index of the goal, which satisfies 5 ≤ N ≤ 100. T is the number of turns. You are requested to compute the probability of success within T turns. T satisfies 1 ≤ T ≤ 100. L is the number of squares marked “Lose one turn”, which satisfies 0 ≤ L ≤ N − 1. B is the number of squares marked “Go back to the start”, which satisfies 0 ≤ B ≤ N − 1. They are separated by a space.

Losei’s are the indexes of the squares marked “Lose one turn”, which satisfy 1 ≤ Losei ≤ N − 1. All Losei’s are distinct, and sorted in ascending order. Backi’s are the indexes of the squares marked “Go back to the start”, which satisfy 1 ≤ Backi ≤ N − 1. All Backi’s are distinct, and sorted in ascending order. No numbers occur both in Losei’s and Backi’s.

The end of the input is indicated by a line containing four zeros separated by a space.

Output

For each dataset, you should answer the probability with which the game succeeds within the given number of turns. The output should not contain an error greater than 0.00001.

Sample Input

6 1 0 07 1 0 07 2 0 06 6 1 1257 10 0 61234560 0 0 0

Sample Output

0.1666670.0000000.1666670.6196420.000000

题意:扔骰子往前走,遇到L停一步,遇到B返回起点,要求在T步内走完,问你到达终点的概率是多少。

思路:因为有步数限制就简单了,dp[i][j]表示第i步停在j位置上的概率。

AC代码如下:

#include<cstdio>#include<cstring>using namespace std;double dp[110][110];int L[110],B[110];int main(){    int t=0,n,m,l,b,i,j,k,p;    double ans;    while(~scanf("%d%d%d%d",&n,&m,&l,&b) && n)    {        t++;        memset(dp,0,sizeof(dp));        dp[0][0]=1;        for(i=1;i<=l;i++)        {            scanf("%d",&k);            L[k]=t;        }        for(i=1;i<=b;i++)        {            scanf("%d",&k);            B[k]=t;        }        for(i=0;i<m;i++)        {            for(j=0;j<n;j++)            {                for(k=1;k<=6;k++)                {                    p=j+k;                    if(p>n)                      p=2*n-p;                    if(L[p]==t)                      dp[i+2][p]+=dp[i][j]/6;                    else if(B[p]==t)                      dp[i+1][0]+=dp[i][j]/6;                    else                      dp[i+1][p]+=dp[i][j]/6;                }            }        }        ans=0;        for(i=0;i<=m;i++)           ans+=dp[i][n];        printf("%.6f\n",ans);    }}



0 0
原创粉丝点击