POJ 1322 概率DP

#include<cstdio>
#include<cstring>
#define LL __int64
const int LMT=55;
LL dp[LMT][LMT];
bool is[LMT][LMT];
void init(void)
{
    memset(is,0,sizeof(is));
    memset(dp,0,sizeof(dp));
}
LL gcd(LL a,LL b)
{
    return b?gcd(b,a%b):a; 
}
int main(void)
{
    int n,m,i,j,cnt,x,y;
    LL base,tem;
    char sec[LMT];
    while(~scanf("%d%d",&n,&m))
	{
       getchar();
        m++;
       init();
        base=0;
      for(i=1;i<=n;i++)
	  {
         gets(sec);
         cnt=0;
      for(j=0;sec[j];j++)
	  {
        if(sec[j]=='*'||sec[j]=='.')++cnt;
        if(sec[j]=='*')is[i][cnt]=1;
	  }
	}
    is[0][0]=1;
    for(i=1;i<=n+1;i++)is[n+1][i]=1;
    dp[0][0]=1;
   for(i=0;i<=n;++i)
     for(j=0;j<=i;++j)
     if(is[i][j])
    {
        x=i+1;y=j;
                   if(y)
     while(x<=n+1&&y<=x)
     {
          if(is[x][y])
		  {
             dp[x][y]+=(1<<(x-i-1))*dp[i][j];
             break;
		  }
      x+=2;++y;
     }
     x=i+1;y=j+1;
     while(x<=n+1&&y<=x)
     {
        if(is[x][y])
		{
          dp[x][y]+=(1<<(x-i-1))*dp[i][j];
          break;
		}
         x+=2;++y;
     }
    }
    for(i=1;i<=n+1;i++)base+=dp[n+1][i];
   tem=gcd(dp[n+1][m],base);
   printf("%I64d",dp[n+1][m]/tem);
   printf("/");
   printf("%I64d",base/tem);
   printf("\n");
 }
 return 0;
}


你可能感兴趣的:(POJ 1322 概率DP)