石油大 2017年省赛前最后一水 1023: Pong’s Birds

来源:互联网 发布:经典文案 知乎 编辑:程序博客网 时间:2024/05/18 19:43

1023: Pong’s Birds

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

题目描述

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.

样例输入

110 0.50.5 0

样例输出

0.500 0.500

分析:经典的dp题目;思路见代码;


代码:


#include<stdio.h>#include<math.h>double map[200][200];double dp[200][200];int main(){    int T,n;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        int maxn=pow(2,n);        for(int i=1;i<=maxn;i++)        {            for(int j=1;j<=maxn;j++)                scanf("%lf",&map[i][j]);        }        for(int i=1;i<=maxn;i++)            dp[0][i]=1.0;        int cnt;        for(int i=1;i<=n;i++)       //一共有n轮比赛        {            for(int j=1;j<=maxn;j++)    //求出每个人在第i轮比赛的取得胜利的概率,最后状态转移到总冠军            {                dp[i][j]=0.0;                int tem=j;                if(i>1)                    tem=ceil((double)(j)/(double)pow(2,i-1));                   cnt=0;                if(tem%2==1)                {                    tem++;  //tem和k计算出pow(2,i-1)个对手所在区间的起始位置                    for(int k=tem*pow(2,i-1);cnt<pow(2,i-1);cnt++,k--)  //pow(2,i-1)控制本轮可能遇到的对手的个数                        dp[i][j] += dp[i-1][j]*map[j][k]*dp[i-1][k];    //晋级的概率*获胜的概率*对手晋级的概率                }                else                {                    tem--;                    for(int k=tem*pow(2,i-1);cnt<pow(2,i-1);cnt++,k--)                        dp[i][j] += dp[i-1][j]*map[j][k]*dp[i-1][k];                }            }        }        printf("%.3f",dp[n][1]);        for(int id=2;id<=maxn;id++)            printf(" %.3f",dp[n][id]);        putchar('\n');    }    return 0;}



0 0