LightOJ1265---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

Output for Sample Input

4

0 0

1 7

2 0

0 10

Case 1: 1

Case 2: 0

Case 3: 0.3333333333

Case 4: 1

每天都会有2种动物相遇
dp[i][j]表示有i只tiger,j只deer时,可以逃脱的概率
当然i=0的话,概率就是=1,这样就可以递推求解了
具体的方程自己想一下很容易就可以写出来

/************************************************************************* > File Name: h.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年05月04日 星期一 14时34分12秒 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

double dp[1010][1010];

int main() {
    int tt;
    scanf("%d", &tt);
    int icase = 1;
    while (tt--) {
        int t, d;
        scanf("%d%d", &t, &d);
        for (int i = 0; i <= t; ++i) {
            for (int j = 0; j <= d; ++j) {
                dp[i][j] = 0.0;
            }
        }
        for (int i = 0; i <= d; ++i) {
            dp[0][i] = 1.0;
        }
        for (int i = 1; i <= t; ++i) {
            for (int j = 0; j <= d; ++j) {
                double r1 = 0, r2 = 0;
                if (i >= 2) {
                    r1 += dp[i - 2][j] * (i * (i - 1)) * 1.0 / ((i + j + 1) * (i + j));
                }
                if (j >= 1) {
                    r1 += dp[i][j - 1] * (i * j * 2.0 / ((i + j + 1) * (i + j)));
                }
                double p = 0;
                if (j >= 2) {
                    p = (j * (j - 1) * 1.0) / ((i + j + 1) * (i + j));
                }
// r1 /= (1 - p - j * 1.0 / ((i + j + 1) * (i + j)));
                if (j >= 1) {
                    r2 = r1;
                    r2 += j * 2.0 / ((i + j + 1) * (i + j)) * dp[i][j - 1];
                    r2 /= (1 - p);
                }
                r1 /= (1 - p - j * 2.0 / ((i + j + 1) * (i + j)));
                dp[i][j] = max(r1, r2);
            }
        }
        printf("Case %d: %.12f\n", icase++, dp[t][d]);
    }
    return 0;
}

你可能感兴趣的:(dp)