ZOJ 3822 Domination (概率DP)

Domination

Time Limit: 8 Seconds      Memory Limit: 131072 KB      Special Judge

Edward is the headmaster of Marjar University. He is enthusiastic about chess and often plays chess with his friends. What's more, he bought a large decorative chessboard with N rows and M columns.

Every day after work, Edward will place a chess piece on a random empty cell. A few days later, he found the chessboard was dominated by the chess pieces. That means there is at least one chess piece in every row. Also, there is at least one chess piece in every column.

"That's interesting!" Edward said. He wants to know the expectation number of days to make an empty chessboard of N × M dominated. Please write a program to help him.

Input

There are multiple test cases. The first line of input contains an integer T indicating the number of test cases. For each test case:

There are only two integers N and M (1 <= N, M <= 50).

Output

For each test case, output the expectation number of days.

Any solution with a relative or absolute error of at most 10-8 will be accepted.

Sample Input

2
1 3 
2 2 

Sample Output

3.000000000000 
2.666666666667 

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5376
题意:说有个傻孩子喜欢痴迷下象棋,并且有一个习惯,每一天随机放一只棋子在(n*m)的棋盘的随意一个位子里,只能放空格子。问当满足任意一行以及任意一列都有棋子时,再不放了。问这样随机放棋子直到放不了棋子的天数的期望
思路: dp[i][j][k]  已经占据i行j列,走了k步的时候,还需要走的步数的期望,放的是概率
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int N = 55;
double dp[N][N][N*N];
int main()
{
    int t,n,m;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(dp,0,sizeof(dp));
            dp[0][0][0] = 1;
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                {
                    for(int k=1; k<=n*m; k++)
                    {
                        dp[i][j][k] += 1.0 * dp[i-1][j-1][k-1] * (n-i+1) * (m-j+1) / (m * n - k+1);
                        dp[i][j][k] += 1.0 * dp[i][j-1][k-1] * i * (m-j+1) / (n * m - k+1);
                        dp[i][j][k] += 1.0 * dp[i-1][j][k-1] * j * (n-i+1) / (n * m - k+1);
                        if(!(i==n&&j==m))//放满了就不再放
                        dp[i][j][k] += 1.0 * dp[i][j][k-1] * (i * j - k+1) / (n * m - k+1);
                    }
                }
            double ans = 0;
            for(int k=0; k<=n*m; k++)
                ans += dp[n][m][k]*k;//*k求天数期望
            printf("%.8lf\n",ans);
        }
    }
}
View Code

第二种写法:推荐
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
const int N = 55;
double dp[N][N][N*N];
int main()
{
    int t,n,m;
    while(~scanf("%d",&t))
    {
        while(t--)
        {
            scanf("%d%d",&n,&m);
            memset(dp,0,sizeof(dp));
            dp[0][0][0] = 1;
            for(int i=1; i<=n; i++)
                for(int j=1; j<=m; j++)
                {
                    for(int k=1; k<=n*m; k++)
                    {
                        dp[i][j][k] += 1.0 * dp[i-1][j-1][k-1] * (n-i+1) * (m-j+1) / (m * n - k+1);
                        dp[i][j][k] += 1.0 * dp[i][j-1][k-1] * i * (m-j+1) / (n * m - k+1);
                        dp[i][j][k] += 1.0 * dp[i-1][j][k-1] * j * (n-i+1) / (n * m - k+1);
                       // if(!(i==n&&j==m))//放满了就不再放
                        dp[i][j][k] += 1.0 * dp[i][j][k-1] * (i * j - k+1) / (n * m - k+1);
                    }
                }
            double ans = 0;
            for(int k=1; k<=n*m; k++)
                ans += (dp[n][m][k]-dp[n][m][k-1])*k;//*k求天数期望  相减求出了概率
            printf("%.8lf\n",ans);
        }
    }
}
View Code
从后往前推:
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define CL(a,b) memset(a,b,sizeof(a))
#define IN(s) freopen(s,"r",stdin)
const int MAXN = 55;
double dp[MAXN][MAXN][MAXN*MAXN];

int main()
{
  //IN("D.txt");
  int ncase;
  scanf("%d",&ncase);
  int n,m;
  while(ncase--)
  {
    scanf("%d%d",&n,&m);
    CL(dp,0);
    for(int i=n;i>=0;i--)
      for(int j=m;j>=0;j--)
      {
        if(i==n&&j==m)continue;
        for(int k=i*j;k>=max(i,j);k--)
        {
          dp[i][j][k]+=1.0*j*(n-i)/(1.0*n*m-k)*dp[i+1][j][k+1];
          dp[i][j][k]+=1.0*i*(m-j)/(1.0*n*m-k)*dp[i][j+1][k+1];
          dp[i][j][k]+=1.0*(i*j-k)/(1.0*n*m-k)*dp[i][j][k+1];
          dp[i][j][k]+=1.0*(n-i)*(m-j)/(1.0*n*m-k)*dp[i+1][j+1][k+1];
          dp[i][j][k]+=1.0;
        }
      }
    printf("%.12lf\n",dp[0][0][0]);
  }
  return 0;
}
View Code
posted @ 2015-07-17 12:19  Doli  阅读(101)  评论(0)    收藏  举报