Pong’s Birds(概率 模拟)

Pong’s Birds

时间限制: 1 Sec   内存限制: 128 MB
提交: 98   解决: 24
[ 提交][ 状态][ 讨论版]

题目描述

In order to train his birds, Pong is holding a competition for them. (He have birds, does he?) He have 2n  birds,
so he want to hold elimination series. Literally, if n = 2 and he has 4 birds identified as 1,2,3,4, he would first hold  competition for 1 and 2, 3 and 4, the one fails would be eliminated and then he holds competition for the winners. 
According to his long observation, for each pair of birds he knows the probability to win for each bird, and he  want to know the probability of each bird to be the final winner.

输入

For the first row there is an integer T(T ≤ 100), which is the number of test cases.
For each case , first row is an integer n(1 ≤ n ≤ 7), for the next 2n  rows , each row has 2n  real numbers as the  probability to win for the i-th when competed to j-th bird. It is promised that for each i, j p[i][j] + p[j][i] = 1 and  p[i][i] = 0;

输出

For each case you should output a line with 2n  real numbers, which is the probability of i-th bird be the final  winner. Rounded to 3 decimal places.
Make sure to output a space character after each number, to prevent the Presentation Error.

样例输入

1
1
0 0.5
0.5 0

样例输出

0.500 0.500

提示


2^n个鸟比赛,1和2,3和4...,然后每个赢的在和相邻的比,输的就被pass掉,然后求每个鸟成为winner的概率。

其实就是个模拟的过程,不过这里真正处理起来还是有点技巧和规律的。


这里有个奇偶互换的想法挺好的!!!


#include 
using namespace std;
 
const int MAXN = (1 << 7) + 5;
double p[MAXN][MAXN];
double dp[8][MAXN];//dp[i][j]表示第i轮j队伍出线概率
 
int main()
{
    int t;
    int n;
    int n2;
    int i, j, k;
    int grp;
    for (j = 0; j < MAXN; ++j) {
        dp[0][j] = 1;
    }
    scanf("%d", &t);
    while (t--) {
        scanf("%d", &n);
        n2 = 1 << n;
        for (i = 0; i < n2; ++i) {
            for (j = 0; j < n2; ++j) {
                scanf("%lf", &p[i][j]);
            }
        }
 
        for (i = 1; i <= n; ++i) {
            for (j = 0; j < n2; ++j) {
                grp = j / (1 << (i - 1));
                grp ^= 1;//奇偶互换
                dp[i][j] = 0;
                for (k = grp << (i - 1); k < ((grp + 1) << (i - 1)); ++k) {
                    dp[i][j] += dp[i - 1][j] * dp[i - 1][k] * p[j][k];
                }
            }
        }
 
        for (i = 0; i < n2 - 1; ++i) {
            printf("%.3lf ", dp[n][i]);
        }
        printf("%.3lf\n", dp[n][n2 - 1]);
    }
    return 0;
}







你可能感兴趣的:(找规律,模拟,动态规划---概率DP)