LightOJ - 1265 Island of Survival (概率dp)

You are in a reality show, and the show is way too real that they threw into an island. Only two kinds of animals are in the island, the tigers and the deer. Though unfortunate but the truth is that, each day exactly two animals meet each other. So, the outcomes are one of the following

a)      If you and a tiger meet, the tiger will surely kill you.

b)      If a tiger and a deer meet, the tiger will eat the deer.

c)      If two deer meet, nothing happens.

d)      If you meet a deer, you may or may not kill the deer (depends on you).

e)      If two tigers meet, they will fight each other till death. So, both will be killed.

If in some day you are sure that you will not be killed, you leave the island immediately and thus win the reality show. And you can assume that two animals in each day are chosen uniformly at random from the set of living creatures in the island (including you).

Now you want to find the expected probability of you winning the game. Since in outcome (d), you can make your own decision, you want to maximize the probability.

Input

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

Each case starts with a line containing two integers t (0 ≤ t ≤ 1000) and d (0 ≤ d ≤ 1000)where t denotes the number of tigers and d denotes the number of deer.

Output

For each case, print the case number and the expected probability. Errors less than 10-6 will be ignored.

Sample Input

4

0 0

1 7

2 0

0 10

Sample Output

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

题意:

有t只老虎,d只小鹿,老虎遇到老虎会自相残杀,老虎遇到小鹿或者遇到你就会痛下杀手,你遇到小鹿可以选择杀或者不杀,每次随机两只生物相遇,问你能活到最后的最大几率.

思路

dp[i][j]表示还剩下i只老虎,j只小鹿的概率.

转移比较简单,处理选到两只小鹿等于没选,所以先把这部分概率去掉.

但是在AC之后的测试中,发现无论小鹿的数量怎么变,最后的概率都是没有变化的.

想了一下确实如此,因为小鹿完全可以把选到小鹿的动作都看做无效的.

#include
#include
#include
#include
#include
#include
#include<set>
#include
#include
#include
#include

#define fuck(x) cerr<<#x<<" = "<#define debug(a, x) cerr<<#a<<"["<#define lson l,mid,ls
#define rson mid+1,r,rs
#define ls (rt<<1)
#define rs ((rt<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int loveisblue = 486;
const int maxn = 1024;
const int maxm = 100086;
const int inf = 0x3f3f3f3f;
const ll Inf = 999999999999999999;
const int mod = 1000000007;
const double eps = 1e-6;
const double pi = acos(-1);

double dp[maxn][maxn];
int main() {
    ios::sync_with_stdio(true);
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif

    int T;
    scanf("%d",&T);
    int cas = 0;
    while (T--){
        int n,m;
        scanf("%d%d",&n,&m);
        if(n&1){
            printf("Case %d: 0\n",++cas);
            continue;
        }
        memset(dp,0,sizeof(dp));
        dp[n][m]=1;
        for(int i=n;i>=0;i-=2){
            for(int j=m;j>=0;j--){
                double sum= 1;
                if(i>0)sum = 1.0-1.0*j*(j+1)/(i+j+1)/(i+j);
                if(i!=0){
                    double tmp = 1.0*i*(i-1)/(i+j+1)/(i+j)/sum;
                    dp[i-2][j]+=dp[i][j]*tmp;
                }if(j!=0){
                    double tmp = 2.0*i*j/(i+j+1)/(i+j)/sum;
                    dp[i][j-1]+=dp[i][j]*tmp;
                }if(i==0&&j!=0){
                    dp[i][j-1]+=dp[i][j];
                }
            }
        }
        printf("Case %d: %.7f\n",++cas,dp[0][0]);
    }
    return 0;
}
View Code

转载于:https://www.cnblogs.com/ZGQblogs/p/11385591.html

你可能感兴趣的:(LightOJ - 1265 Island of Survival (概率dp))