石油大OJ 1023 问题 L: Pong’s Birds【概率Dp】

351 篇文章 2 订阅

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.

样例输入

1
1
0 0.5
0.5 0

样例输出

0.500 0.500


题目大意:

给你2^N只鸟.现在进行2^N-1场比赛绝出冠军,问你每只鸟获得冠军的概率。
给出2^N只鸟互相战斗的胜率。
比赛规则:
相邻的两只鸟进行战斗,假如现在有1 2 3 4
一开始1和2战斗,3和4战斗。如果:2和4胜利了。那么2和4绝出冠军。

思路:

概率问题,暴力肯定要超时,那么我们考虑概率Dp.
设定Dp【i】【j】表示第i轮比赛,j获得了胜利的概率。

那么就有:

Dp【i】【j】=Dp【i-1】【j】*Dp【i-1】【k】*a【j】【k】.
这里j和k要在这一轮有可能战斗上才行。

所以预处理出来N轮比赛每只鸟可以和哪些鸟战斗即可。

Ac代码:


#include<stdio.h>
#include<string.h>
#include<vector>
#include<math.h>
using namespace std;
vector<int >mp[10][513];
double dp[10][513];
double a[513][513];
int color[10][513];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=(int)pow(2,n);i++)
        {
            for(int j=1;j<=(int)pow(2,n);j++)
            {
                scanf("%lf",&a[i][j]);
            }
        }
        memset(color,0,sizeof(color));
        for(int i=1;i<=n;i++)
        {
            int fanwei=(int)pow(2,i);
            int cnt=0;
            for(int j=1;j<=(int)pow(2,n);j+=fanwei)
            {
                cnt++;
                for(int k=j;k<j+fanwei;k++)
                {
                    color[i][k]=cnt;
                }
            }
        }
        memset(dp,0,sizeof(dp));
        for(int i=1;i<=(int)pow(2,n);i++)dp[0][i]=1;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=(int)pow(2,n);j++)
            {
                for(int k=1;k<=(int )pow(2,n);k++)
                {
                    if(j==k)continue;
                    if(color[i][j]==color[i][k])
                    {
                        if(i==1)
                        {
                            dp[i][j]+=dp[i-1][j]*dp[i-1][k]*a[j][k];
                        }
                        else
                        {
                            if(color[i-1][j]!=color[i-1][k])
                            {
                                dp[i][j]+=dp[i-1][j]*dp[i-1][k]*a[j][k];
                            }
                        }
                    }
                }
            }
        }
        for(int i=1;i<=(int )pow(2,n);i++)
        {
            if(i>1)printf(" ");
            printf("%.3lf",dp[n][i]);
        }
        printf("\n");
    }
}





















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值