poj 3071 Football 概率DP
概率DP,递推式:dp[i][j]=sigma(dp[i-1][j]*p[j][k]*dp[i-1][k])
再就是判断j和k是否相邻:if(((k>>(i-1))^1)==(j>>(i-1)))
代码如下:
#include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> #include<cmath> #include<cstring> using namespace std; double dp[129][129],p[129][129]; int main() { int i,j,k,ans,n; while(cin>>n){ if(n==-1) break; for(i=0;i<(1<<n);i++) for(j=0;j<(1<<n);j++){ cin>>p[i][j]; } memset(dp,0,sizeof(dp)); for(i=0;i<(1<<n);i++) dp[0][i]=1; for(i=1;i<=n;i++) for(j=0;j<(1<<n);j++) for(k=0;k<(1<<n);k++){ if(((k>>(i-1))^1)==(j>>(i-1))) dp[i][j]+=dp[i-1][j]*dp[i-1][k]*p[j][k]; } ans=0; for(i=0;i<(1<<n);i++) if(dp[n][i]>dp[n][ans]) ans=i; cout<<ans+1<<endl; } }