zoukankan      html  css  js  c++  java
  • LightOJ 1030

    题目链接:https://cn.vjudge.net/problem/LightOJ-1030

    You are in a cave, a long cave! The cave can be represented by a 1 x N grid. Each cell of the cave can contain any amount of gold.

    Initially you are in position 1. Now each turn you throw a perfect 6 sided dice. If you get X in the dice after throwing, you add X to your position and collect all the gold from the new position. If your new position is outside the cave, then you keep throwing again until you get a suitable result. When you reach the Nth position you stop your journey. Now you are given the information about the cave, you have to find out the expected number of gold you can collect using the given procedure.

    Input

    Input starts with an integer T (≤ 100), denoting the number of test cases.

    Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the dimension of the cave. The next line contains N space separated integers. The ith integer of this line denotes the amount of gold you will get if you come to the ith cell. You may safely assume that all the given integers will be non-negative and no integer will be greater than 1000

    Output

    For each case, print the case number and the expected number of gold you will collect. Errors less than 10-6 will be ignored. 

    Sample Input

    3

    1

    101

    2

    10 3

    3

    3 6 9

    Sample Output

    Case 1: 101.0000000000

    Case 2: 13.000

    Case 3: 15

    题意:

    给出n个格子,编号为1~n,每个格子里有价值Gi的宝藏;

    现在,扔一个六面的标准的骰子,按得到的点数走格子,起点为1;

    如果在某个格子,扔出骰子之后,得到的点数会让你走到编号大于n的格子,就不算数,重新扔,直到扔到一个你能走点数为止;

    如果你走到了编号n的格子,就停止;

    求得到宝藏价值的期望值。

    题解:

    每个格子的价值为Gi,我们只要求出每个格子可能被走到的概率Pi,那么我们求出Σ( Gi * Pi )即为答案;

    显然是个概率DP题,动态转移求出走到每个格子的概率即可;

    AC代码:

     1 #include<cstdio>
     2 #include<cstring>
     3 #define MAXN 105
     4 #define min(a,b) (a<b)?a:b
     5 int n,grid[MAXN];
     6 double dp[MAXN];
     7 int main()
     8 {
     9     int t;
    10     scanf("%d",&t);
    11     for(int kase=1;kase<=t;kase++)
    12     {
    13         scanf("%d",&n);
    14         for(int i=1;i<=n;i++) scanf("%d",grid+i);
    15 
    16         memset(dp,0,sizeof(dp));
    17         dp[1]=1;
    18         for(int i=1;i<=n;i++)
    19         {
    20             int k=min(6,n-i);
    21             for(int j=1;j<=k;j++) dp[i+j]+=dp[i]*(1.0/k);
    22         }
    23 
    24         double ans=0;
    25         for(int i=1;i<=n;i++) ans+=dp[i]*grid[i];
    26         printf("Case %d: %.7lf
    ",kase,ans);
    27     }
    28 }

    PS. becky大佬有期望DP的做法:http://blog.csdn.net/becky_w/article/details/78247858

  • 相关阅读:
    谋哥:这个时代没有比程序员更适合创业
    《人人都可以创业》连载1:创业很简单,从心开始
    谋哥:悟马道长第一句话之“不要赚屌丝的钱”
    谋哥:App开发者的苦逼不值得怜悯!
    我们都傻不啦叽为《围住神经猫》免费推广!
    谋哥:转型之痒与App推广之痛
    学习IOS开发项目篇--如何让程序在后台保持挂起状态
    学习IOS开发项目篇--SDWebImage基本使用
    学习IOS开发网络多线程篇--NSThread/GCD/
    学习IOS开发UI篇--UICollectionViewController的基本使用
  • 原文地址:https://www.cnblogs.com/dilthey/p/7684262.html
Copyright © 2011-2022 走看看